Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Need Help on Generecs and non-Generics  RSS feed

 
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 ]
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the formal parameter of the insert method is a raw List.
 
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
Boost this thread!