• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between List generic types

 
Joe Harry
Ranch Hand
Posts: 10109
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

What is the difference betweeb List<?> and List<Object>?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34210
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jothi,
"?" means any type. This means you cannot make updates to the list. Where "object" means any object so you can add objects to the list.
 
Joe Harry
Ranch Hand
Posts: 10109
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that ? wildcard stands for any type. I also know that with List<Object> I can add to the List only Objects..not a String, not a Integer and so on. But with List<?>, you mean to say I cannot add anything? Am I right?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:
I also know that with List<Object> I can add to the List only Objects..not a String, not a Integer and so on. Am I right?
No. You can add Integers, Strings, Foos or anything else to a List<Object>. You might have to cast them when they come out, which rather defeats the purpose of using generics in the first place, however.
 
camilo lopes
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with <?> you can't add nothing the list. but with <Object> you can add all object that extends Object.
in my blog there is one post about this situation but in portuguese, you translate for english.

 
Joe Harry
Ranch Hand
Posts: 10109
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, got the difference. When we cannot add anything to a list when we say List<?>, then why does the compiler allow to create one such thing?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you declare a List as unknown type (List<?>) and you initialise it to a particular type?

You will have to go through the standard references, viz the Java Tutorials and do a Google search for Angelika Langer who has a website with FAQs about Java generics.
[ September 15, 2008: Message edited by: Campbell Ritchie ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34210
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:
When we cannot add anything to a list when we say List<?>, then why does the compiler allow to create one such thing?

Creating one in the same line you are instantiating the list isn't very useful. List<?> comes in handy when it is used as a method parameter. method(List<?> list) allows me to take any type of list in provided a promise not to edit it.
 
Joe Harry
Ranch Hand
Posts: 10109
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Surely you declare a List as unknown type (List<?>) and you initialise it to a particular type?

You will have to go through the standard references, viz the Java Tutorials and do a Google search for Angelika Langer who has a website with FAQs about Java generics.

[ September 15, 2008: Message edited by: Campbell Ritchie ]


The link was very useful. Thanks! :thumb:

[edit]Disable smilies. CR[/edit]
[ September 16, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome It is worthwhile bookmarking the Java Tutorials; you will use them frequently.
 
Joe Harry
Ranch Hand
Posts: 10109
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got this below code from here,

Example (of unchecked cast):



How does the code commented as //unchecked warning succeed?

[edit]disabled smilies and minor spelling correction. CR[/edit]
[ September 17, 2008: Message edited by: Campbell Ritchie ]
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you're first removing the type information, then adding a different one. The latter is allowed so you can use legacy code - code written before generics existed.

It's also why you get the warnings - the compiler can't guarantee type safety anymore.
 
Joe Harry
Ranch Hand
Posts: 10109
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really did not get your explanation. Can you be a bit clear?
 
Joe Harry
Ranch Hand
Posts: 10109
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Surely you declare a List as unknown type (List<?> and you initialise it to a particular type?

You will have to go through the standard references, viz the Java Tutorials and do a Google search for Angelika Langer who has a website with FAQs about Java generics.

[ September 15, 2008: Message edited by: Campbell Ritchie ]


I once again would like to thank you for the links. They are damn helpful!
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:
I really did not get your explanation. Can you be a bit clear?

You have a List<Date>. You then cast it to an Object, which is just perfectly legal. After all, every non-primitive IS-A Object in Java. However, then you are casting it to List<String>. The compiler gives you a warning. It doesn't give you an error because the object could be a List<String>. It can't tell though.

Now why this gives a warning and casting without generics (e.g. (Date)object) doesn't is because of type erasure. In the byte code created by the compiler, the <Date> part is removed. It effectively is just a List. Now during runtime you can check if an object is a Date or List (using instanceof), but you can't check if an object is a List<Date> - because the <Date> part is no longer there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic