aspose file tools
The moose likes Java in General and the fly likes What is the difference between of using Collection<?> and Collection<Object> 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 » Java » Java in General
Reply Bookmark "What is the difference between of using Collection<?> and Collection<Object>" Watch "What is the difference between of using Collection<?> and Collection<Object>" New topic
Author

What is the difference between of using Collection<?> and Collection<Object>

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear All,

I want to know the difference between Collection<?> and Collection<Object>



they are telling
Object is not a super type of all kinds of collections


Please can you elaborate this statement because Object is super of all the kinds as i know


Creativity is nothing but Breaking Rules
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14670
    
  11

You can use the Edit button () to edit your post. I have copy/pasted your second message into the first one this time.


[My Blog]
All roads lead to JavaRanch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Collection<Object> is just that - a Collection with generic type Object. You can retrieve elements as Object references and put any object in it.
Collection<?> is a Collection with an unknown generic type. It could be Integer, String or anything else. Since the type is unknown elements can only be retrieved as references to the common super type -- Object. However, since you don't know the type you can't add anything but null. Anything else might lead to problems later on (e.g. if the actual generic type is Integer and you add a String -- oops!).

So that's the difference: with <?> you can't add anything, with <Object> you can.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: What is the difference between of using Collection<?> and Collection<Object>
 
Similar Threads
Interface and array
Constructor inheritance
method call
Generics understanding
use of casting