• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics doubt

 
Filipe Pinheiro
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
Sudhakar Sharma
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator


Thank you Edwin for the crystal clear answer.
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic