• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the Edit button () to edit your post. I have copy/pasted your second message into the first one this time.
 
Sheriff
Posts: 22854
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic