File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics" Watch "generics" New topic
Author

generics

Prahlad Joshi
Ranch Hand

Joined: Apr 21, 2007
Posts: 44
(while List list=new ArrayList<Integer> is fine as to support preJava 5 code)
in K&B it is written that List<anyType> can refer to only lists of that "anytype" (unless we use wild card notations) but it also compiles

List<a> list=new ArrayList();

with warning
Note: C:\Documents and Settings\Administrator\JavaApplication1\src\javaapplication1\test4.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

what is the concept behind this.....
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The concept here is that you are allowed to mix typed and raw Collections.

Unless you suppress warnings, you will get that warning when you compile it.
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
The below description is given by one bartender. I think it helps you.
=========================================================================

Case one:

code:


List<Integer> a = new ArrayList(); //warning
a.add(1);
Integer i = a.get(0);
System.out.println(i);



The first line produces a warning.
Because variable a has a generic type List<Integer> and whatever you get out of the list is guaranteed to be an Integer. Therefore the third line will compile and the whole snipped would output a 1.

Why is it allowed to have the ArrayList non-generic here?
Because old classes compiled with java 1.4 or lower still have to work, and an old 4.1 class can easily have an ArrayList as return type.

Case two:

code:


List a = new ArrayList<Integer>();
a.add(1); // warning
Integer i = a.get(0); //
System.out.println(i);



Here a is of raw type. It stores only objects and you can retrieve only objets from that collection. Even if the object is of type ArrayList<Integer>.
Therefore you get now a compiler error in the third line. Because the cast (Integer) is missing.
You have to insert it manually (in comparison to generics on both side, the compiler would do this for you behind the scenes).
And now the second line produces a warning. Compiler also would warn you if the first line were List a = new ArrayList(); (raw type on both sides).
Because only the compile type matters, and it is non-parameterized.
You can store anything into it. A line a.add("howdy"); in this context would compile also.
Again this "half-generics" is also allowed because it must be guarantied that generic and legacy code can interact.


SCJP-1.5<br />SCWCD-1.4
Prahlad Joshi
Ranch Hand

Joined: Apr 21, 2007
Posts: 44
thanks dolly for posting the full concept(it really helped) and thanks keith .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics