• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Generics doubt

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folks,

I have not understood why the following statement doesn't generate a compile warning:



But, this does:



Looking foward an answer,

Filipe.
 
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think "List L=new ArrayList<Integer>();" will first create a Arraylist of Integer typed class then upcast to the type of Object, which is default. but whenever you are trying like "List<Integer> L=new ArrayList();" it will try to create a Object typed ArrayList first then try to set them into a Integer type List.
In first case it will compile and show a warnning conserning about you list is no more type-safed. But in second case compiler will generate an error.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This does not generate a compiler error, just a warning, though.

This is like this because Java did not have generics until JDK1.5, and they wanted that legacy code were compatible with new generics code. Therefore, if you had libraries returning raw types or receiving raw types as parameters, the Java Programming Language should still work.

This practice though is not recommended for new code written for JDK15 or superior. What you did should have been done like this



A list like this can accept any kind of list and since it uses an unbounded wildcard it will not generate a warning, as it does your legacy code.
 
Sudhakar Sharma
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thank you Edwin for the crystal clear answer.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assigning a generic collection to a raw type is not exactly good practice, but it's not dangerous either. Whatever you get out of that collection, you're gonna have to cast it anyway.

Now, if you assign a raw type to a Generic Collection, you're looking at unexpected ClassCastExceptions at runtime. Hence the warning.
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic