wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Raw to <? extends Object> 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 "Raw to <? extends Object>" Watch "Raw to <? extends Object>" New topic
Author

Raw to <? extends Object>

Kim Ming Yap
Ranch Hand

Joined: Dec 17, 2008
Posts: 53

Hi,

I am having problem understanding the below:

1) List a = new ArrayList();

2) List <?> b = a;

3) List <? extends Object> c = a; // with warning of course

Why there is an unchecked warning at clause 3) while no unchecked warning at 2) ?
I don't see the difference between <?> (Unbounded wildcard) and <? extends Objects> (Since Object is the highest upper bound in Java)

Can anyone help me on this?
Thanks.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

You may want to look here -
How polymorphism works in Generic


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

Sunny Jain wrote:You may want to look here -
How polymorphism works in Generic


Even though I read this article, I didn't understand why this occurs.

Does the List<?> the same thing than List<? extends Object> ???

Could anyone explain it in more clear way please?

tks


Adolfo Eloy
Software Developer
OCPJP 6
Kim Ming Yap
Ranch Hand

Joined: Dec 17, 2008
Posts: 53

Yea .. That article doesn't address my question. It explains type erasure in response to polymorphism.

My question is simple.
The hierarchy is as follows:

1. Raw type
2. Unbounded wild card <?>
3. <? extends Object>

if you read Java by Mughal (java version 6 page 678), from the above .. raw is the highest follow by <?>.
However <?> is equaivalent to <? extends Objects> in terms of hierarchy.

Now in terms of narrowing conversion, raw to <?> there is no unchecked conversion warning.
From <?> to <? extends Objects> there is also no unchecked conversion warning.
However from raw to <? extends Objects> there is unchecked conversion warning.

This is where i don't understand since to me, <? extends Objects> has the highest upper bound in Java (which is Objects).
So to me <?> is equivalent to <? extends Objects> and should behave the same way for both.

ANY ONE WHO CAN HELP ME???
Eric Kizaki
Greenhorn

Joined: Jan 21, 2011
Posts: 12

This really is not answering your question. I was able to replicate the warning you had. This warning only occurs when using a legacy list. When I used generic code with the generic modifiers I did not get any warnings.



I agree it is kinda weird. I think all you need to know for the exam is that using legacy code with generics produces warnings.


OCPJP 6
Kim Ming Yap
Ranch Hand

Joined: Dec 17, 2008
Posts: 53

I don't think it's weird. It's confirmed in the book Java by mughal.

Had it been something like <? extends Numbers> in other words not Objects (which is the highest class in Java) i would understand the unchecked warning. But since Object is the highest class in the class hierarchy, i don't see a problem. So i don't see why the unchecked warning pops up. There must be a reason for it.

Could it be that as long as it is bounded (? extends something - irregardless if that something is Object or not), the compiler enforced the unchecked warning anyway?
This is the only explanation i can think of.

Else there must be a reason for this behaviour.
By the way i'm not studying for the exam.
I just want to know. Thanks.
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19728
    
  10

Kim Ming Yap wrote:Had it been something like <? extends Numbers> in other words not Objects (which is the highest class in Java) i would understand the unchecked warning. But since Object is the highest class in the class hierarchy, i don't see a problem.


Here you are suggesting that the language definition should have a special paragraph which says that Object will be treated differently than any other class in the construction you're asking about. Language definers prefer not to do that. I haven't read the Java Language Specification but my guess is that the language definers didn't do that in this case. I suggest you look there, though, if you want to find the definitive answer.
Kim Ming Yap
Ranch Hand

Joined: Dec 17, 2008
Posts: 53

ok .. I did checked the java 6 specification .. but no answer was found there.
Sign ..
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580


statement 1 says, 'a' is a list that take any type of objects.
statement 2 says, 'b' is a list that take any type of objects.
basically i think , the declarations:

is exactly same.

statement 3 says, List 'c' can take any object that is a sub type of object class . But it does not take a list of Objects.
so when you do : c=a, the compiler is says you might have a list of Objects in 'a' and you are assigning it to 'c' which does not take list of Objects

Hope i am right , correct me if not.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Kim Ming Yap
Ranch Hand

Joined: Dec 17, 2008
Posts: 53

No. That is incorrect.

A <? extends Objects> means that it is a subclass of Object including Object itself. So Objects is the upper bound. So <? extends Objects> can store practically everything just like <?>.
Still waiting for an answer to my question.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Kathy Sierra 6 .SCJP book . In Page 618 of the book that i have wrote:
public void addAnimal(List<? extends Animal> animals)

By saying <? extends Animal>, we're saying, "I can be assigned a collection
that is a subtype of List and typed for <Animal> or anything that extends Animal.
And oh yes, I SWEAR that I will not ADD anything into the collection."
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3989
    
  18

Kathy Sierra 6 .SCJP book . In Page 618 of the book that i have wrote:
public void addAnimal(List<? extends Animal> animals)

By saying <? extends Animal>, we're saying, "I can be assigned a collection
that is a subtype of List and typed for <Animal> or anything that extends Animal.
And oh yes, I SWEAR that I will not ADD anything into the collection."


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Raw to <? extends Object>
 
It's not a secret anymore!