File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mixing generic &  nongeneric 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 "mixing generic &  nongeneric" Watch "mixing generic &  nongeneric" New topic

mixing generic & nongeneric

Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
class test034
public static void main(String[] args)
List <Integer> ll=new ArrayList<Integer>();
ll.add(new Integer(65));
changelist cl=new changelist();
class changelist
{void changeit(List li)
li.add(new Integer(57));
li.add(new Integer(87));


above code compiles without errors but with warnings:"Note: Recompile with -Xlint:unchecked for details. "
According to K&B page 577,if the list is of type Integer and we pass an Integer object to it will compile without warning and any should give a warning only when we try to push in an object other than stated by K & B.please help on this minute detail.
thanks in advance
Anit Nair

Joined: May 09, 2008
Posts: 12
Dear friend,

Whenever you pass a generic collection type to a non generic collection type, as you are doing in the ex. mentioned, there are two scenarios to be taken into consideration.
1)You are just reading the values
2)Are you modifying the collection within the function

1) In this case the code will run without warning
2) In this case regardless of the type of Object you are adding, it will compile with warning. The reason being since you are passing a generic collection type to a non-generic type (ie pre-Java 5) the Java compiler warns that object you are adding may cause problems. This is because there is no way to detect what the type of object is at runtime. Because all the generic information is compile time information.

Hope this helps.
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
checks please:
when show this message stand for that you used 1.5 with 1.4. but your code compile and execute normally.

Brazil - Sun Certified Java Programmer - SCJP 5 About Java - Update every Week.
Guide SCJP - tips that you need know
I agree. Here's the link:
subject: mixing generic & nongeneric
jQuery in Action, 3rd edition