9

What is the difference between Class<?> and Class<Object> in Java? AFAIK Java Erasure changes <?> to it's upper bound, which in this case would be Object anyway. So what is this for?

alex
  • 10,900
  • 15
  • 70
  • 100
  • 2
    Intuitively, it feels wrong to have a `Class` be anything else than `Object.class`. Mainly because it reminds me of the many, many questions that are of the form "I want to assign a `List` to a `List` but Java won't let me." When writing code, you shouldn't primarily care what generics will erase to - the entire point of generics is to let the compiler do some type checks *before* they're erased. – millimoose May 05 '13 at 23:29
  • possible duplicate of [What is the difference between ? and Object in Java generics?](http://stackoverflow.com/questions/678822/what-is-the-difference-between-and-object-in-java-generics). Also see [Java: Different between List, List>, List, List, and List](http://stackoverflow.com/questions/6231973/java-different-between-list-list-listt-liste-and-listobject) – Paul Bellora May 06 '13 at 03:28
  • I think this http://docs.oracle.com/javase/tutorial/java/generics/unboundedWildcards.html explains it pretty well if I understood the question correctly. – arynaq May 05 '13 at 23:35
  • `Class> cls = int.class;// Compiles` `Class cls2 = int.class;// Can't convert from Class to Class` It's a matter of what you can put into the variable. – Kröw Jul 04 '18 at 18:41

1 Answers1

7

the difference is that Collection<String> is not a subtype of Collection<Object>, Collection<?> is usable in place as an argument where any collection can be put

aaronman
  • 18,343
  • 7
  • 63
  • 78
  • Fixed it, better take that down vote off – aaronman May 06 '13 at 02:26
  • 2
    What is wrong with this – aaronman May 06 '13 at 02:48
  • 1
    OP is asking for Class and you are answering with Collection, that's one problem. The other problem is, Collection> is **not** the supertype for all collections. I think you have misunderstanding wildcard in generics. – Adrian Shum May 06 '13 at 03:35
  • Actually mine is correct now, http://www.cs.cmu.edu/~pattis/15-1XX/15-200/lectures/collectionsv/ – aaronman May 06 '13 at 03:38
  • 1
    However I see your point about "answering with Collection" even though your english is somewhat difficult to understand, In regards to his question I assumed because of what I said that there was some parallel situation for Collection> as there is for Class>, but maybe not – aaronman May 06 '13 at 03:40
  • Actually yours is NOT correct, even base on the link you quoted. Class>/Collection> is NOT supertype for all Class/Collection. Foo> matches with Foo with any type parameters, doesn't mean it is a supertype of all Foo. They are simply different concepts that you shouldn't mix up. – Adrian Shum May 06 '13 at 04:18
  • 2
    http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html, c'mon man – aaronman May 06 '13 at 04:27
  • 2
    There is a quote in this article confirming what I said – aaronman May 06 '13 at 04:42
  • 3
    @aaronman thats a good explanation, dont know who voted down for you... looks like Adrian Shum never programmed java.... – Dima May 06 '13 at 06:32
  • 1
    well, if you actually understand the wildcard in Java Generics, you know why I said that's incorrect. In your quoted article, the "supertype" is only restrict on the context about the type matching with any type param (i.e. Foo> matches Foo). However, it has nothing to actually do with the type hierarchy. i.e. Collection> is not in the type hierarchy of any collection type, hence saying it to be supertype of any collection is simply wrong. – Adrian Shum May 06 '13 at 08:11
  • 1
    Also, given this simple example: `List> fooList = ...; List> barList = ...; fooList.addAll(barList);` is not valid, obviously you cannot simply say List> is the supertype: if List> is actually a "supertype" for Lists, there is no reason you cannot add the "same" type of list to another. However, the given code is not working. – Adrian Shum May 06 '13 at 08:13
  • Ok, this is the first thing you've said that is actually helpful, why don't you start proposing edits instead of leaving this long trail of comments – aaronman May 06 '13 at 15:40
  • I have been making my point clear from the very beginning and, please don't say "this is the first thing... actually helpful" just because you can't understand what other people said. And, given the answer is fundamentally incorrect imho, I don't think it is appropriate to propose edits. I am leaving comment here just to tell why this answer is incorrect. If now you understand why your answer is incorrect, it is more appropriate for you yourself to amend the answer. – Adrian Shum May 07 '13 at 01:10
  • @AdrianShum: Your example does not contradict that `List>` is a supertype of `List`. Nowhere does it say that the argument to `.addAll()` is a subtype of the type of the variable it's called on. Also, by type hierarchy you're talking about the *class* hierarchy. But nobody said that "supertype" or "subtype" had to do with the class hierarchy. The concept of supertype/subtype for generic types is a relation that does not have to correspond to a hierarchy structure. – newacct May 09 '13 at 07:33
  • @AdrianShum: See Java Language Specification 4.10.2 (http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.10.2) "The direct supertypes of the parameterized type C, where Ti (1 ≤ i ≤ n) is a type, are all of the following: [...] C, where Si contains Ti (1 ≤ i ≤ n)." "Contains" is defined in section 4.5.1 (http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.5.1) which clearly specifies that a wildcard contains any type that satisfies its bounds. – newacct May 09 '13 at 08:10
  • @newacct That's a nice piece of information. I am not sure if what I am saying is actually *class* hierarchy because interface is also in the picture. The concept in the JLS imho is more about covariance which I always treat it different from type hierarchy. And, this is probably my prejudice that when I am talking about "subtype/supertype", the term itself give me a hint it is about type hierarchy. :P However seems it is not the way it is treated in JLS (for which subtype/supertype is used to describe the covariance) – Adrian Shum May 09 '13 at 09:18
  • Thanks a lot for the information and probably I need to adjust my use of supertype/subtype to describe covariance instead of type hierarchy – Adrian Shum May 09 '13 at 09:20