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

Generics Generics Generics !!!

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Which is valid and which is Invalid and why?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both are valid. But both will generate a compiler warning. The second one will generate a compilation warning if you try to modify the collection...
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Both are valid. But both will generate a compiler warning...



I think second one will not generate compiler warning.
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please confirm whether 2nd one will generate warning or not?
First one generates.

P.S: I don't have JDK access.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Singh wrote:

Ankit Garg wrote:Both are valid. But both will generate a compiler warning...



I think second one will not generate compiler warning.



I thought I would correct that before anyone sees it ...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good one Ankit
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prav just see this code

 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok

Means 2nd line will generate warning only if we try to add something to it.

If we don't have add for second list then we won't get any warning???
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you think Prav why there is compiler warning in second line add() and why not in first line add() method?
And also

List<String> list1 = new ArrayList(); //warning

why this compiles with warning, while this below one not?

List list2 = new ArrayList<String>();
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it because that the first declaration tells clearly that Strings can be added Whereas second list is not specifying anything ?

If its like that then what does the RHS of second declaration conveys? Doesn't it serve any purpose?

Punit Singh wrote:Can you think Prav why there is compiler warning in second line add() and why not in first line add() method?
And also

List<String> list1 = new ArrayList(); //warning

why this compiles with warning, while this below one not?

List list2 = new ArrayList<String>();

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prav wrote:If its like that then what does the RHS of second declaration conveys? Doesn't it serve any purpose?



It is serving its purpose na, but in this case it is not deciding what can be added.

Reference type always restricts what you can assign to this reference type, and what can you add if reference type is collection, or what method can be called. This is the power of reference type, RHS is a small child against the power of LHS, LHS says what is possible and what is not always.

List<String> list1 = new ArrayList(); //warning

Here also its reference type is scolding RHS why it is assigning such an ArrayList(), that has no parameter restriction.
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it

Thanks Guys !!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic