Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Katrin Perry
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • 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
 
Tomaszz Lewandowski
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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()
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic