Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Generics Again

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When compiling with the -Xlint option, we see, for the first code segment -



For the second -



Regards,
Dan
 
Whizlabs Java Support
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you friends for replying.... thanks !!
 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic