aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics 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" Watch "Generics" New topic
Author

Generics

Katrin Perry
Ranch Hand

Joined: Mar 07, 2007
Posts: 61
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


Katrin
SCJP 5.0, SCWCD, SCBCD, Oracle Certified Master Enterprise Architect
Tomaszz Lewandowski
Ranch Hand

Joined: Sep 07, 2007
Posts: 30
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()


Regards<br />Tomasz Lewandowski<br />SCJP5 (97%) | SCWCD5 (98%)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

Note: You didn't need to post a new topic about your question; you could have edited your old post by clicking the button.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Katrin Perry
Ranch Hand

Joined: Mar 07, 2007
Posts: 61
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

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Katrin Perry
Ranch Hand

Joined: Mar 07, 2007
Posts: 61
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics