jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Difference between List generic types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between List generic types" Watch "Difference between List generic types" New topic
Author

Difference between List generic types

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Guys,

What is the difference betweeb List<?> and List<Object>?


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30369
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

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

Joined: Oct 13, 2005
Posts: 38481
    
  23
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

Joined: Aug 08, 2007
Posts: 202
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.



Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

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

Joined: Oct 13, 2005
Posts: 38481
    
  23
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30369
    
150

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

Joined: Sep 26, 2006
Posts: 9351
    
    2

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

Joined: Oct 13, 2005
Posts: 38481
    
  23
You're welcome It is worthwhile bookmarking the Java Tutorials; you will use them frequently.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

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

Joined: Oct 27, 2005
Posts: 19680
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

I really did not get your explanation. Can you be a bit clear?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

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

Joined: Oct 27, 2005
Posts: 19680
    
  18

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between List generic types