ए भाई Think ज़रा हटके
Note: amtyThumb must be installed for new version of amty thumb post/recent

Java Generics – Boys are not allowed in Girls community

In this article you’ll know How to use, Why to use, Where to use generics. You’ll get the answer for what problem we face while using generics with solution, explanation and examples.
I hope short stories and images, used in this article, will keep your interest alive and will increase your understanding.

Syntax

Old generation – Without Generic

List myIntList = new LinkedList(); // 1
myIntList.add(new Integer(0)); // 2
Integer x = (Integer) myIntList.iterator().next(); // 3

New generation – With Generic

List<Integer> myIntList = new LinkedList<Integer>(); // 1
myIntList.add(new Integer(0)); //2
Integer x = myIntList.iterator().next(); // 3

Purpose:

Generics are invented for type safety and to improve readability. Without generic, you are allowed to add any type of object to a List (collection) which you are supposed to type cast further while accessing. (See old generation syntax block above). It leads “chance of type casting exception” at run time. If a programmer is not aware about what type of elements are expected from the list, he can add any type of element to it. For example;


Mr. Ramesh was accessing employee from Person list. But Mr. Smith was unaware with Ramesh program. So he was adding students to Person list and passing it to Ramesh application.


Java class inheritance - generic

With generics this type of confusions can be averted easily. But if you really require that the Person list must allow employee as well as students but not else, generic has provision for that as well.

How to use

  1. When you create an object of a generic class, or you call a generic method you have to tell him the data type (see new generation syntax block above).
  2. When you define/declare a method/class you have to use generic type or specific data type. We’ll study it later.

Now problem begins….

Generics are not covariant. They are invariant
Arrays in the Java language are covariant — which means that if Integer extends Number (which it does), then not only is an Integer also a Number, but an Integer[] is also a Number[], and you are free to pass or assign an Integer[] where a Number[] is called for. (More formally, if Number is a supertype of Integer, then Number[] is a supertype of Integer[].)
But in generic List is not a supertype of List. You can’t pass a List where a List is expected.

List<Integer> inLi = new ArrayList<Integer>();
List<Number> numLi = inLi; // illegal

Explanation:

 

  1. List says that you can add any type of number (Float,Integer,Long) to the list numLi.
  2. But second statement depicts that numLi; pointing to inLi; can have only Integer type.

Both statements contradict each other. So the compiler itself doesn’t allow any contradict statement which depicts type unsafety. So the second statement is not allowed. Instead

List<Integer> inLi = new ArrayList<Integer>();
List<? extends Number> numLi = inLi; // legal
  1. You can pass any element/variable/object of type Number. Here extends is used for extend & implement both.
  2. ? stands for wildcard. It means any class.

Note that

List does not mean “list of objects of different types, all of which extend Number”. It means “list of objects of a single type which extends Number


java Generic example boy and girl

This area is protected to registered users only.
Well! There is one more keyword super. In summary,

List<String> is a subtype of List<? extends Object>
List<Object> is a subtype of List<? super String>

Another example;
This area is protected to registered users only.
Point to be noticed my loard
This area is protected to registered users only.
Intersting… Isn’t it? Let’s study more.

You cannot instantiate an array of a generic type (new List[3] is illegal). For example, the following example is illegal;

List<String>[] stringList = new List<String>[10]; // illegal

Why so?

As we know that every class is the subtype of Object class. And an array of Object class can have array of any class. And where “any” word comes, generic fails.
Since an array of object can have stringList

Object[] objArr = stringList;  // OK because List<String> is a subtype of Object

It can have list of other class type too. But clever generic never takes a chance. So the above statement is wrong. Instead,

Object[] objArr = new Object[10];

And below statements will work fine then

List<Integer> inLi = new ArrayList<Integer>();
inLi.add(new Integer(3));
objArr [0] = inLi;

Are you still not able to remember it? Ok ok just imagine an Object class as a Cricket Team. You can create team of boys as well as girls. But as per the rule, Boys team will play against boys team only and so on.
This area is protected to registered users only.


Java Generic girls vs boys cricket

Alternate
Even though you are not allowed to instantiate generic array, you can declare them in following manner;

List<String>[] stringList = (List<String>[]) new List<?>[10];
stringList.add(“amit gupta”);


Picture अभी बाकी हे मेरे दोस्त …. wait for my next post

*This article is under copyright. Publishing images, code, examples any where on net or book without permission and credit to original article is strictly prohibited.

Amit Gupta

Hey! this is Amit Gupta (amty). By profession, I am a Software Eng. And teaching is my passion. Sometimes I am a teacher, as you can see many technical tutorials on my site, sometimes I am a poet, And sometime just a friend of friends...

1658
views


To book below area mail me




captcha

You can follow any responses to this entry through the RSS 2.0 feed.