Can any body explain this generic vector Vector<? super Object> list = ... and where to use it?
- 7,006
- 27
- 78
- 121
-
4Most consider it deprecated. It's basically a synchronized List... http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated – Peter Svensson Sep 11 '12 at 12:01
-
`? super Object` is an Object really... – assylias Sep 11 '12 at 12:04
-
4I wouldn't use Vector, I would use List and ArrayList. I wouldn't use `? super Object` which is the same as `Object` as its plain confusing. – Peter Lawrey Sep 11 '12 at 12:05
4 Answers
It is the same as
Vector<Object> list
Usually "? super Type" means that you can add supertypes of type Type
Object is the highest level in Java so there are no supertypes so the ? super is not needed
If it was
Vector<? extends Object> list
then it would mean that you can add any object that is a subclass of Object. This is every object in Java so it would be the same as
Vector<Object> list
Also consider List rather than Vector and if you want it thread safe then wrap it in Collections.synchronizedList(...)
- 15,272
- 18
- 86
- 131
Here you are defining the lower bound of your unknown object ?. So the Vector<? super Object can contain only Object and any super class of Object. But since Object does'nt have a super class it has no sense. It behaves same as Vector<Object>.
Refer this sample.
The counterpart for this is upper bound Vector<? extends Object> where you can add any object that extends Object.
You should be able to avoid using Object as the generic type is most cases.
- 4,777
- 4
- 24
- 41
-
Does it means if i have my own object it will be useful to use it?if yes how can it be useful? let say like this example: `List super MyObject> list = ...` – itro Sep 11 '12 at 12:25
-
Yes. In that case you can add `MyObject` and any parent classes of `MyObject`. – basiljames Sep 11 '12 at 12:32
<? super Object> is absurd since there is no super type of Object, but it is allowed, and it consists of just Object types. http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html
- 5,231
- 6
- 34
- 40
Such a list can only contain elements of type Object as Object doesn't have any super types similar to using extends of with an immutable class like String where such a collection can contain only Strings.
- 23,934
- 10
- 61
- 84