jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Need Help on Generecs and non-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 "Need Help on Generecs and non-Generics" Watch "Need Help on Generecs and non-Generics" New topic

Need Help on Generecs and non-Generics

swarupa patil
Ranch Hand

Joined: Feb 22, 2006
Posts: 62
Hi Ranchers,
I have dobut on Generics and non-Generics
I have written the code that have Generics as well as the non-generics
part.The code below compile fine but gives one warning even though
the list is of Integer genric type and I am trying to add the element
of Integer type but still it gives one warning.

If I try to add the element as shown above at Line 111 it gives
compile time waring

I need information why the above code gives the compiletime warnings
[ September 26, 2006: Message edited by: Barry Gaunt ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Note that the formal parameter of the insert method is a raw List.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Let's trim your example down to only the class Inserter:

The compiler warning (use -Xlint to get more info) is:

What this means is that the compiler is going to allow you to add an Integer to the list even though you have not specified what type of objects the list contains. You may have passed in a list containing only Strings and after performing the add(new Integer(42)) you are going to have a mixture of Strings and Integers. This may cause problems later when you use the list. For example, if you take an object from the list thinking that it is a String but it happens to be that Integer, then you will get a ClassCastException at runtime. Putting it simply, the warning is telling you that the compiler cannot check if you will or will not get a ClassCastException definitely because it does not know how you are intending to use the list in the future.
[ September 26, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link: http://aspose.com/file-tools
subject: Need Help on Generecs and non-Generics
It's not a secret anymore!