Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help on Generecs and non-Generics

 
swarupa patil
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the formal parameter of the insert method is a raw List.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic