0

I was going through generics in Java and I'm having trouble trying to understand where I would use the following two.

I understand that the first myList would ensure that the list only contains elements of type Integer and all it's superclasses. Now I'm trying to figure out where myList2 would fit in here.

List<? super Integer> myList;
List<Class<? super Integer>> myList2;

Edit: It's not a duplicate of the question being linked...since this is clearly regarding the use of ? vs Class<? whereas the other question is about super vs extend

asm
  • 837
  • 1
  • 16
  • 41
  • `myList2.add(Integer.class);` – Ruslan Mar 25 '19 at 21:32
  • Possible duplicate of [What is a difference between super E> and extends E>?](https://stackoverflow.com/questions/1368166/what-is-a-difference-between-super-e-and-extends-e) – LppEdd Mar 25 '19 at 21:32
  • 2
    `myList` is a list of **object instances**, e.g. a `List` storing `Integer`, `Double`, `BigDecimal`, ... objects, such as `42`, `3.14159`, `3e9876`, ... --- `myList2` is a list of **`Class` objects**, i.e. a list of `Integer.class`, `Number.class`, `Object.class`, `Serializable.class`, or `Comparable.class` values. – Andreas Mar 25 '19 at 21:33
  • I understand the difference between super and extends. I was just wondering where myList2 would be used. – asm Mar 25 '19 at 21:33
  • To be exact, the second one contains objects whose type is Class\, not classes of type Integer. 'Class' is a generic class, objects of that class are objects, not classes. –  Mar 25 '19 at 21:46
  • 1
    I can't offhand think of a use for a list of class objects, but in general you use a Class object as a way to indicate type, or as a way to determine the properties of that type. –  Mar 25 '19 at 21:53

2 Answers2

1

The first one, 'myList' may contain integer values. Example: myList.add(200);

The second one, 'myList2' may contain classes of type integer. Example: myList2.add(Integer.class);

Update: As correctly pointed out in the comments, due to the "super" keyword being applied, in addition to objects/ classes of type Integer, all superclasses of the type Integer (that is: Number and Object) can be contained as well.

Andreas Vogl
  • 1,776
  • 1
  • 14
  • 18
  • Ok so like you mentioned in the comments, if instead of Integer, I'm extending Number, then the second list could contain elements Integer, BigDecimal etc. – asm Mar 25 '19 at 21:34
  • The first list might also contain e.g. `String` values, because it might be a `List`. --- The second list can also store classes of other types, e.g. `Number.class`, `Object.class`, `Serializable.class`, and `Comparable.class`. – Andreas Mar 25 '19 at 21:35
0

The first list contains objects of Integer and its superclasses.
The second one contains class objects (or simply classes) of Integer and its superclasses, i.e:

Integer.class;
Number.class;

You can read more about the differences here: The difference between Classes, Objects, and Instances

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28