0

In the Java generics tutorial it says that <?> in a generic method means "unknown".

But I don't understand how <?> is different than <T>. Both mean that you can pass in any type-parameter you'd like. Please explain this operator.

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • @MarounMaroun I now did some reading on this site (that I probably should have done earlier), and I think I maybe realize the difference now. Please say if this is correct: in a generic method, `>` means this works with anything. `` also means this works with anything, but is used if you need to refer to the type `T` later on in the code. Correct? – Aviv Cohn Sep 14 '14 at 12:27
  • You define `T` during declaration like in `List`. That way you set `T` to `String` and every method that accepts an argument of type `T` you'll have to pass String and or a subtype of it (in case of String, there is no subtype). `?` means that you pass everthing, no matter what you passed before. – Tom Sep 14 '14 at 12:33
  • Take a look at the following link for a nice explanation about the difference between wildcards and generic types: http://stackoverflow.com/questions/10943137/difference-between-generic-type-and-wildcard-type – Moonlit Sep 14 '14 at 12:36

1 Answers1

0

The following excerpts come from http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

And another:

The unbounded wildcard type is specified using the wildcard character (?), for example, List. This is called a list of unknown type. There are two scenarios where an unbounded wildcard is a useful approach:

  • If you are writing a method that can be implemented using functionality provided in the Object class.
  • When the code is using methods in the generic class that don't depend on the type parameter. For example, List.size or List.clear. In fact, Class is so often used because most of the methods in Class do not depend on T.

The second bullet makes the biggest distinction. Use '?' When you don't need to call operations on the actual type. See the link provided if you need more details. There are several sub sections under this wild cards section that cover different points so don't stop with just the page you are directed to.

klog
  • 486
  • 3
  • 10
  • 1
    Hmm, that second quote is actually pretty poor, given it's the official tutorial. The rationale for wildcards is definitely not just "when you don't need to use the type". – Oliver Charlesworth Sep 14 '14 at 12:50
  • Would you mind elaborating on the rationale? extends SomeClass> gives you more access to an object and if you actually are using SomeObject then you have all of the access, but if you are using a derived object this indicates that you don't need to access the functionality of '?' – klog Sep 14 '14 at 13:08
  • A method `foo(List> list)` can take a `List` or a `List`; the same cannot be said for `foo(List list)`. – Oliver Charlesworth Sep 14 '14 at 13:13