aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics Again" Watch "Generics Again" New topic
Author

Generics Again

vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
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

Joined: Jul 09, 2001
Posts: 1180
When compiling with the -Xlint option, we see, for the first code segment -



For the second -



Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
James X Peterson
Whizlabs Java Support
Ranch Hand

Joined: Feb 26, 2013
Posts: 158
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

Joined: Apr 06, 2010
Posts: 4467
    
    8

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

Joined: Jul 12, 2010
Posts: 103
Thank you friends for replying.... thanks !!
vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Again