This week's giveaway is in the Testing forum. We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line! See this thread for details.
When I run this from the CMD first line compiles with warnings ,second compiles without (although they both produce warnings on eclipse).I am very confused because the first declaration of ArrayList can only take Strings ,but second one can take any type of object which will make a mess at runtime so shouldn't it be the other way round? Could you please explain to me this issue because I'm so confused and please correct me if I'm wrong.
I am a little confused about what you are trying to ask. Are you asking if line 1 or line 2 should not compile or if one of them should not throw a warning message ?
S Ali wrote:
When I run this from the CMD first line compiles with warnings ,second compiles without (although they both produce warnings on eclipse).I am very confused because the first declaration of ArrayList can only take Strings ,but second one can take any type of object which will make a mess at runtime so shouldn't it be the other way round? Could you please explain to me this issue because I'm so confused and please correct me if I'm wrong.
second one can take any type of object which will make a mess at runtime so shouldn't it be the other way round?
I'm able to add any object to this list : List l2 = new ArrayList<String>();
And it does not give any runtime exceptions also. Then what is the effect of initializing ArrayList<String>(). It is the same as without the generic declaration. Am I right?
What exactly is a generic.....type safety and what is the broader class for a generic type that is a raw type.
Generic instantiation assigned to a raw reference type. This compiles without warnings as the compiler knows only about the reference type. I will create a class cast exception at runtime if the programmer has a cast like (Integer)l2. This is allowed at compile time as l2 is a raw type and the list may contain Integer which is not known to the compiler.
This following can be seen as unsafe as the arraylist can contain many other things but the compiler allows only String.
This code can be viewed as in
First the compiler is upset that you are assigning a raw type to a generic type but the compiler cannot do anything but give a warning because both the classes are the same after type erasure.
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Murtuza Arzai wrote:
I'm able to add any object to this list : List l2 = new ArrayList<String>();
And it does not give any runtime exceptions also. Then what is the effect of initializing ArrayList<String>(). It is the same as without the generic declaration. Am I right?
Yes. If you declare List l2 = new ArrayList<String>() then you lose all the safety benefits provided by generics. The row types are only allowed because of compatibility. In general, you should never use row type such as List in your code.
The compiler will issue a warning only it thinks that it can't ensure type safety (as Nitish mentioned). The warnings are issued if some of the statements might result in a run-time exception (and thus break type safety). Let's take the statements one by one
Here the compiler issues warning. As Anastasia explained this is because the right hand operand might contain a List of any type of objects (like below).
After this point it is the responsibility of the compiler to ensure that l contains only elements of type String. But since the list might already contain a different type that String (like in the second code), thus later a get() operation on the typed list might result in a ClassCastException (thus break type safety). This is why the compiler issues a warning as this statement might break type safety
Now lets examine the other statement
This time you don't get any warning. This is because you are just assigning the typed list to an untyped list. You'll only get a warning if you try to add anything to l2. This is because that might break type safety somewhere else
This is why the compiler issues a warning when you try to add an element to an untyped collection...