1

Look, we know that on compilation generic classes are transformed into raw form, so

List<String> a=new Arraylist<String>

is effectively the same for

List<String> a=new Arraylist<>

Am i right? Are there any situations when it is really needed to specify the type inside diamond on the right?

SubZr0
  • 137
  • 7

5 Answers5

3

From Java 7 onwards, you won't need to specify the type on instantiation part. For return statements also the same applies. It is implicitly taken from return type.

Naveed S
  • 5,106
  • 4
  • 34
  • 52
  • >It is implicitly taken from return type. But does it matter? There is one raw type for all possible type arguments, and behaviour of object is set by reference type. Why is it needed? – SubZr0 Nov 14 '16 at 09:24
2

You may want to have a look at Oracle's official documentation on the topic.

Since Java 7, as you guessed, it is not necessary to specify the type as long as it can be obviously inferred by the context. To quote the most relevant part:

Java SE 7 supports limited type inference for generic instance creation; you can only use type inference if the parameterized type of the constructor is obvious from the context. For example, the following example does not compile:

List<String> list = new ArrayList<>(); list.add("A");

  // The following statement should fail since addAll expects
  // Collection<? extends String>

list.addAll(new ArrayList<>());

EDIT: a more comprehensive Oracle tutorial on type inference, up to date with Java 8 enhancements.

walen
  • 7,103
  • 2
  • 37
  • 58
1

Am i right? Are there any situations when it is really needed to specify the type inside diamond on the right?

First of all, yes if you're using java 1.6 or lower because diamond operator didn't exists.


Java 7 or later, is not necesary, it reduces verbosity in order to better readability injecting types when necessary (instantiation, return types...)

FROM THIS ARTICLE

In other words, the JDK 7 Project Coin addition of a Diamond Operator brings type inference to constructors that has been available with methods.

  • With methods type inference is implicitly done when one leaves off the explicit parameter type specification.
  • With instantiation, on the other hand, the diamond operator must be specified explicitly to "tell" the compiler to infer the type.
joc
  • 1,336
  • 12
  • 31
0

The <> is called the Diamond Operator. The Diamond Operator reduces some of Java's verbosity surrounding generics by having the compiler infer parameter types for constructors of generic classes. In other words, since Java 7 it is not necessary to specify the type inside the diamond operator.

See: What is the point of the diamond operator in Java 7?

Community
  • 1
  • 1
Andrei Olar
  • 2,270
  • 1
  • 15
  • 34
0

You are mixing a few things up here.

we know that on compilation generic classes are transformed into raw form

Basically true, it's called type erasure.

List<String> a=new Arraylist<String> is effectively the same for List<String> a=new Arraylist<>

Not in the sense of type erasure. Simply put, when compiling, List<String> a=new Arraylist<String>, is "changed" to List a=new Arraylist

The example you have brought up, is a new feature in Java 7. You can ommit the generic parameter on the right hand side of the assignment und just use the so called "diamond operator". This is where your statement becomes true. Prior to Java 7, you cannot do this.

André Stannek
  • 7,773
  • 31
  • 52
  • Ok, if when compiling, List a=new Arraylist, is "changed" to List a=new Arraylist Why can't be performed 'a.add(new Exception("dummy"))'? Is correctness of method checked before type erasure? – SubZr0 Nov 14 '16 at 09:41
  • 1
    @SubZr0 Correct. At compile time, the generic types are used for such checks. Type erasure is performed after those checks. – André Stannek Nov 14 '16 at 09:51