• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Raw to <? extends Object>

 
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to look here -
How polymorphism works in Generic
 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Kim Ming Yap
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Greenhorn
Posts: 12
Google Web Toolkit Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kim Ming Yap
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok .. I did checked the java 6 specification .. but no answer was found there.
Sign ..
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Kim Ming Yap
Ranch Hand
Posts: 53
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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."

 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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."

 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic