• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Need Help on Generecs and non-Generics

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic