Question

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>();
Was it helpful?

Solution

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.

OTHER TIPS

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>();
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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top