This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "mixing generic &  nongeneric" Watch "mixing generic &  nongeneric" New topic
Author

mixing generic & nongeneric

Shashank Sharma
Ranch Hand

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

}
[//Code]

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 errors.it should give a warning only when we try to push in an object other than Integer.as stated by K & B.please help on this minute detail.
thanks in advance
Anit Nair
Greenhorn

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:
link
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
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mixing generic & nongeneric