montss
montss

Reputation: 484

What is the difference between <> and <?> in Java generic types?

When you create a collection field that takes a generic type you can do that by giving it ? as the value or nothing at all what is the difference between those:

e.g.

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

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

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

Upvotes: 1

Views: 139

Answers (4)

m-szalik
m-szalik

Reputation: 3654

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

is equivalent to

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

Operator <> is called diamond and was introduced in JDK7

The first statement in your question is invalid.

Upvotes: 1

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95499

The first statement is just wrong; you cannot instantiate a parameterized class with an unknown type. You are likely confusing it with a similar syntax where the unknown is on a declared type as in this dummy example:

public static String getClassName(Class<?> clazz) {
   return clazz.getName();
}

In the example above, the use of <?> indicates that any type instantiation may be provided.

The last two statements are equivalent; in new ArrayList<>(), the String type parameter is inferred by the compiler from the declared type on the left-hand side, while in the last statement this is given explicitly. As others have correctly noted, this type inference is only available as of Java 7. Prior to Java 7, you can achieve similar type inference by taking advantage of the way that types are inferred on method invocations; for example, Guava's Lists.newArrayList() allows one to write "Lists.newArrayList()" on the right-hand side without specifying the type a second time, and pre-JDK7 compilers will properly infer the type from the left-hand side there.

Upvotes: 4

Manuel Spigolon
Manuel Spigolon

Reputation: 12880

the first line is a wild card and the last two are short-code introduced with java 7 and they are the same oracle source

Upvotes: 0

Christoph Schubert
Christoph Schubert

Reputation: 1124

Your second example is a new feature introduces with Java 1.7. The empty generic bracers implicitly take the Generic Type of the left side. So basicly it is the same as your last example.

Additionaly your first example is worng (as the others already stated). Valid would be

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

Upvotes: 1

Related Questions