23

I found a 'unusual' generic syntax such as:

Arrays.<String>asList(...);
Collections.<String>emptyList();

Obviously, the results of the methods are generic. Is such syntax for type checking? An Object array cannot be an argument to Arrays.<String>asList(...).

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130

4 Answers4

27

<typearg>methodname is the syntax for explicitly specifying the type argument for a generic method

When you use a generic class, you usually have to specify the type argument (e.g. String):

ArrayList<String> list =  new ArrayList<String>();

With a generic method, you don't usually pass a type argument:

public static <T> void foo(T param) {   }
...
String s = ...;
MyClass.foo(s);

You'll notice no where did we did the code explicitly specify we want the String version of foo, i.e. there was no explicit type argument <String> specified like we saw when using a generic class (List<String>).

The compiler is doing some compiler magic to infer the generic type argument based on context. This is a great thing and very powerful.

However, occassionally the compiler can't infer the type arguments automatically:

public static <T> void bar() { T myLocalVar = ...; ...  }
MyClass.bar();

What concrete version of bar are we trying to invoke, i.e. what is the type argument for this call? Well, the compiler doesn't either. We have to explicitly state the type argument, just like we normally do when using a generic class:

MyClass.<String>bar();

Also see:


Aside: it may be worth mentioning that the Java 7 will be adding the so-called diamond operator to allow us to have the compiler to infer the type arguments when using generic classes now too:

ArrayList<String> list =  new ArrayList<String>();

becomes

ArrayList<String> list =  new ArrayList<>();

What is the point of the diamond operator (<>) in Java 7?

peterh
  • 11,875
  • 18
  • 85
  • 108
Bert F
  • 85,407
  • 12
  • 106
  • 123
8

This is how you explicitly specify the type parameter to a generic method. In most cases, the compiler can infer it, but sometimes it needs to be explicitly stated.

theChrisKent
  • 15,029
  • 3
  • 61
  • 62
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • For my own curiosity - does it need to be explicitly stated because those methods are static? – christophmccann Jan 28 '11 at 15:04
  • 1
    @Christopher: no. It works both in static and non-static methods. – 卢声远 Shengyuan Lu Jan 28 '11 at 15:07
  • It needs to be explicitly stated because it's static, so there's no constructor which has fixed the Generic type arguments and because the Generic type arguments cannot be inferred from the parameter list. – Edwin Buck Jan 28 '11 at 15:08
  • I mean though - does the compiler infer the type when it is non-static e.g. because you create ArrayList – christophmccann Jan 28 '11 at 15:08
  • 1
    @Christopher: Non-static methods rarely (if ever) have type parameters of their own. If they have, the same issue arises. – Michael Borgwardt Jan 28 '11 at 15:20
  • 1
    @Christopher McCann Inference on types does not happen (yet). (There is such a thing as a generic constructor, but can't actually remember seeing one in the wild.) In JDK7, you should be able to use the diamond operator (`<>`) to write `new ArrayList<>()` and the inference will happen by magic. – Tom Hawtin - tackline Jan 28 '11 at 15:30
  • @Tom: I think Christopher was referring to the inference of type parameters to methods, which *does* already happen most of the time. – Michael Borgwardt Jan 28 '11 at 15:38
  • `Non-static methods rarely (if ever) have type parameters` => `Collection` classes define a generic toArray method with its own type parameter: ` T[] toArray(T[] a); }` – Bert F Jan 29 '11 at 02:52
1

The answers above pretty much address your question, but if you want a specific example of a case where Java's generic type inference fails and explicitly stating it in this way saves the day, consider the following class definitions:

public class A { }
public class B extends A { }
public class C extends A { }

Then the following code works just fine (i.e., Java's type inference succeeds):

List<Class<? extends A>> list = ImmutableList.of(B.class, C.class);

But for the following, it fails to compile:

List<Class<? extends A>> list = ImmutableList.of(B.class);

That's right; strangely enough, by removing a parameter, we confuse the type inference system, since the 'nearest common descendant' of B.class and C.class is A.class, but for B.class by itself, it's just B.class, which (by lack of covariance in Java's generics) does not match List<Class<? extends A>>. In this case, you have no choice but to use:

List<Class<? extends A>> list = ImmutableList.<Class<? extends A>>of(B.class);

Since B does indeed extend A, this compiles (and runs) just fine.

I hope this demonstration emphasizes the usefulness of the operator.

Adrian Petrescu
  • 16,629
  • 6
  • 56
  • 82
0

In addition, after Java 7 compiler support type inference. Consider the following class definitions:

public class A { }
public class B extends A { }
public class C extends A { }

All the following cases are working:

List<Class<? extends A>> list0 = Arrays.asList(B.class, C.class);
List<Class<? extends A>> list1 = Arrays.asList(B.class);

List<A> list2 = Arrays.asList(new B());
List<A> list3 = Arrays.asList(new B(), new C());


List<? extends A> list4 = Arrays.asList(new B(), new C());
List<? extends A> list5 = Arrays.asList(new B());
Max Peng
  • 2,879
  • 1
  • 26
  • 43