Win a copy of Fixing your Scrum this week in the Agile 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Generics

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
posted Yesterday 10:13 PM
--------------------------------------------------------------------------------
Hi,

I came across a question in the Whizzlab test, whose answer I have found confusing.

According to it this code wouldn't compile:



but this one would compile:


My understanding is that:


mean the same thing as


but not according to Whizzlab

Could anyone clarify please?

Thank you
Katrin
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't construct wildcard parametrized type, like this: new LinkedList<? extends Object>(). You must construct concrete parametrized type object or raw type object, like:
new LinkedList<Object>() or
new LinkedList()
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: You didn't need to post a new topic about your question; you could have edited your old post by clicking the button.
 
Katrin Perry
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper the reason why I re-posted it was because it showed that this message has been replied to and nobody answered it.
Thanks anyway
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

Katrin wrote:


but this one would compile:


and then Katrin concluded from this behaviour that a List<Object> and a List<? extends Object> is more or less the same thing.

No, it is not.
Your list contains only objects (a String, an Integer, a Boolean and another String).
Must compile, because you can store everything into an Object.

Compare with this:

This compiles also.

Back to your list of objects:
You can put Strings, Integers etc inside easily, but what you get back from the list is an object. When you want eg to multiply the second entry with 3, then you first have to cast:


prints 12

Hope that helped.

Yours,
Bu.
 
Katrin Perry
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies

I am a bit clearer why:

1.I can't use wildcards when creating objects:



2.I also understand that if you add to a List any types of objects i.e. String, Integer ... you get out Object and you need to cast.

What I am not clear about still is what diffference usind wildcards makes in the following example:



compare to



In my mind in both cases you would have added to the list various shapes (subclasses of Shape) but you would be getting out objects of type Shape, which you would print.



I would be very grateful if someone sheds a bit of light on this concept.

Thanks
Katrin
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic