• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Again

 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Namaste Friends !!

Please explain why the code below compiles with warning ,

and the code below compiles without warning ,


I am too weak in generics so please explain...........

thanks
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When compiling with the -Xlint option, we see, for the first code segment -



For the second -



Regards,
Dan
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A generic class is defined with the following format:

class name<T> { /* ... */ }

To reference the generic Box class from within code, you must perform a generic type invocation, which replaces T with some concrete value, such as Integer:


You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument — Integer in this case — to the Box class itself.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of them like this.
This is always safe. An ArrayList<Integer> is always a List. It's not very good practice - if you're using a generic class you should use generics when possible - but the assignment is entirely safe.

This is not safe. a might reference an ArrayList<Integer>, but it might reference an ArrayList<String>, or a plain ArrayList, etc. It might well contain things that aren't Integers. So the compiler can't make any guarantees about the assignment.

The only reason this is only a warning and not a compiler error is that this is needed in order to cope with legacy code that doesn't use generics. A pre-Java-1.5 library might have methods that returns non-generic Lists and we want to be able to still use it. So the compiler downgrades it to a warning - telling you to avoid it if you can.

 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you friends for replying.... thanks !!
 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Think of them like this.
This is always safe. An ArrayList<Integer> is always a List. It's not very good practice - if you're using a generic class you should use generics when possible - but the assignment is entirely safe.

This is not safe. a might reference an ArrayList<Integer>, but it might reference an ArrayList<String>, or a plain ArrayList, etc. It might well contain things that aren't Integers. So the compiler can't make any guarantees about the assignment.

The only reason this is only a warning and not a compiler error is that this is needed in order to cope with legacy code that doesn't use generics. A pre-Java-1.5 library might have methods that returns non-generic Lists and we want to be able to still use it. So the compiler downgrades it to a warning - telling you to avoid it if you can.



Thank you JAVA-GURU for your valuable explanations
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic