aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Are List<Object> and List<? extends Object> the same thing? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Are List<Object> and List<? extends Object> the same thing?" Watch "Are List<Object> and List<? extends Object> the same thing?" New topic
Author

Are List<Object> and List<? extends Object> the same thing?

Robert O'Leary
Ranch Hand

Joined: Mar 24, 2009
Posts: 41
They both can accept a reference type of any Object or subclass right?


SCJP6
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Yes..you are right.

But only List<Object> allows you to add elements.


SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Robert O'Leary
Ranch Hand

Joined: Mar 24, 2009
Posts: 41
Himalay Majumdar wrote:Yes..you are right.

But only List<Object>allows you to add elements.


Cool...and if you were using them as a method parameters, only List<? extends Object> can accept an argument other than List<Object>e.g as in Line 5.

Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
:deleted:
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Robert O'Leary wrote:
Cool...and if you were using them as a method parameters, only List<? extends Object> can accept an argument other than List<Object>e.g as in Line 5


Not true.. List<? extends Object> can accept ANY argument..including..List<Object>



Check out this link for more practice
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Himalay,

I think you and Robert are saying the same thing. Maybe there is a little misunderstanding going on.

As for whether List<? extends Object> is the same as List<Object>, they are similar but quite different. You basically can't add anything to List<? extends Object>.

I however think that List<? super Object> and List<Object> are exactly the same. If I'm mistaken, someone will correct me.


All code in my posts, unless a source is explicitly mentioned, is my own.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Hi Ruben..how are you doin buddy.

Robert mentioned..List<? extends Object> can accept an argument other than List<Object>.

but List<? extends Object> can accept List<Object>

I dont see any difference between List<? super Object> and List<Object>,

but List<? super Dog> and List<Dog> are not same.
Rex Tan
Greenhorn

Joined: Mar 26, 2009
Posts: 8
Himalay Majumdar wrote:Hi Ruben..how are you doin buddy.

Robert mentioned..List&lt;? extends Object&gt; can accept an argument other than List&lt;Object&gt;.

but List&lt;? extends Object&gt; can accept List&lt;Object&gt;

I dont see any difference between List&lt;? super Object&gt; and List&lt;Object&gt;,

but List&lt;? super Dog&gt; and List&lt;Dog&gt; are not same.



List<? super Object> is very similar to List<Object>

but given Foo extends Object

List<? super Foo> list = new ArrayList<Object>(); is legal
List<Foo> list = new ArrayList<Object>(); is not legal

also for method calls

public void doStuff(List<Foo> l)
{
l.add (new Foo()); //Legal
}

public void doStuff(List<? extends Foo> l)
{
l.add (new Foo()); //Not legal
}

public void doStuff(List<? super Foo> l)
{
l.add(new Foo()); //Legal
}

It's really confusing but.. good luck


SCJP 1.6, SCWCD 1.5, MCTS, MCT
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Adding to Rex's code





Thats the reason I said

List<? super Object> is SAME as List<Object>,

but List<? super Dog> and List<Dog> are NOT SAME.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Himalay Majumdar wrote:Hi Ruben..how are you doin buddy.

Robert mentioned..List&lt;? extends Object&gt; can accept an argument other than List&lt;Object&gt;.

but List&lt;? extends Object&gt; can accept List&lt;Object&gt;

I dont see any difference between List&lt;? super Object&gt; and List&lt;Object&gt;,

but List&lt;? super Dog&gt; and List&lt;Dog&gt; are not same.

Himalay,

Doing good man, how's the studying going?

I think Robert didn't mean that List<? extends Object> can't accept Object references, I think he meant that the only difference between List<? extends Object> and List<Object> is that List<? extends Object> can accept a reference of a type other than Object. But maybe it's me who misunderstood.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Rex Tan wrote:
List<? super Object> is very similar to List<Object>

Hi Rex,

Actually I think they are exactly the same, because the only thing the wildcard can be is Object, since Object doesn't have a superclass. But for other classes, then List<? super A> would be different from List<A>. I think Himalay said the same thing also.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Yeah Ruben..I dint interpret his statement as whole... By "other than List<Object>" he means everything including List<Object>.
Now his code makes sense to me.

By the way..April 4th is my Judgement day..how about you.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Himalay Majumdar wrote:Yeah Ruben..I dint interpret his statement as whole... By "other than List&lt;Object&gt;" he means everything including List&lt;Object&gt;.
Now his code makes sense to me.

By the way..April 4th is my Judgement day..how about you.

I am sure you will do great, you have come a long way since the beginning of the journey.
I haven't set up a date yet myself, but it will be soon.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Thank you so much Ruben. Will keep you posted.
Robert O'Leary
Ranch Hand

Joined: Mar 24, 2009
Posts: 41
Himalay Majumdar wrote:
Robert O'Leary wrote:
Cool...and if you were using them as a method parameters, only List&lt;? extends Object&gt; can accept an argument other than List&lt;Object&gt;e.g as in Line 5


Not true.. List&lt;? extends Object&gt; can accept ANY argument..including..List&lt;Object&gt;



Check out this link for more practice


yep that was i saying, i could have been clearer though! thanks for the discussion gents
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are List<Object> and List<? extends Object> the same thing?
 
Similar Threads
Question from Dan's exam
For Each ArrayList of Object in JSTL
Generic types
Hibernate Query...
tabular data structure in java