Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Are List<?> and List<? extends Object> are absolutely identical?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have confuse.
Can someone explain to me? What is difference and What is the same?
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Panumat

Yes, List<?> and List<? extends Object> are identical
List<?> is known as list containing any object
List<? extends Object> is know as any object which is subclass of Object or actual Object class
 
Panumat Wiwatmaikul
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think so.

But I have read below:
"By the way, List<? extends Object> and List<?> are absolutely identical!
They both say, "I can refer to any type of object." But as you can see, neither of
them are the same as List<Object>
."

From "Scjp Sun Certified Programmer For Java 6 Study Guide (Exam 310-056)".

I just confuse about "!".

So as above sentence it means :

- List<? extends Object> and List<?> are absolutely identical
- List<? extends Object> same as List<Object>

Correct me if I'm wrong.

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, In my opinion above sence means:
- List<? extends Object> and List<?> are absolutely identical
- List<? extends Object> NOT same as List<Object>
- List<?> NOT same as List<Object>
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Panumat Wiwatmaikul wrote:I think so.

But I have read below:
"By the way, List<? extends Object> and List<?> are absolutely identical!
They both say, "I can refer to any type of object." But as you can see, neither of
them are the same as List<Object>
."

From "Scjp Sun Certified Programmer For Java 6 Study Guide (Exam 310-056)".

I just confuse about "!".

So as above sentence it means :

- List<? extends Object> and List<?> are absolutely identical
- List<? extends Object> same as List<Object>

Correct me if I'm wrong.




- List<? extends Object> and List<?> are absolutely identical

- List<? extends Object> is not same as List<Object>


that is because the wild card character enables us to use 'polymorphism' in the generics.
It means that we can extend from a supertype to create a subtype.

Here - List<? extends Object> - means that List can contain Object as well as subclasses of the supertype Object

whereas List<Object> means that List will contain only elements of the Type Object strictly and it will not ne able to implement polymorphism in this case.
 
Panumat Wiwatmaikul
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all guy.

You made me clearly and ensure now.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WIll try to explain this with an example. @ Piya, you also take a look.



Conclusion: The type of listA and listB are identical, but neither is equivalent to the type of listC.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adeel Ansari wrote:WIll try to explain this with an example. @ Piya, you also take a look.



Conclusion: The type of listA and listB are identical, but neither is equivalent to the type of listC.




I don't understand why when you try to add an String Object or even a new Object() to the listA or listB you receive a compilation error. It is supposed that String extends from Object and new Object() is an instance of class Object isn't it?.

Any suggestions?

Cheers
 
Tom Kowalski
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually this kind of syntax you use as parameter of function, for example:



The function doesn’t know what kind of list you will send to this function, it can be, for example List<Dog> or List<Cat>. So compiler won’t let you to add anything to this kind of list. Because in this case, it would turned out that you add (for example) a String to a list, while it should have only ‘Dog’ elements.




The only thing you can add to list which is declared as List<?> or List<? extends Object> is null.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Victor Fernandez wrote:
I don't understand why when you try to add an String Object or even a new Object() to the listA or listB you receive a compilation error. It is supposed that String extends from Object and new Object() is an instance of class Object isn't it?.

Any suggestions?

Cheers



Well, because you cannot add a String, or even an Object to a List<? extends Object>, since it might be a list of some other subtype of Object. Whereas, the snippet below would work like charm.



Okay, another elaboration. Suppose this code snippet below.




Whereas with super



I hope this effort would make the thing clear.

Cheers.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. The upper bound wildcard parameterized type (<? extends object>) are equivalent but they are NOT “absolutely identical”

Here is a case that you can see the difference:

 
Victor Fernandez
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I hope this effort would make the thing clear.



I got the point, anyway I'll do some snippets to clarify this concepts.

Thanks!
 
I love a good mentalist. And so does this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic