0

just like the title said, in Java, is there any difference between the two statements?

Usually I can see the two statements both. So I suppose there may be no difference. But could you help to confirm and explain why they are identical?

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

Thanks!

user2864740
  • 57,407
  • 13
  • 129
  • 202
Zhe Hou
  • 55
  • 6
  • they are identical just two ways of writing. – Panther Sep 11 '15 at 04:33
  • 1
    Hi, all, I want to thank you all so much for your answers!! Just mark the first one as acceptable answer, but I think all your answers are clear and correct identically. – Zhe Hou Sep 11 '15 at 06:28

3 Answers3

1

They are identical. new ArrayList<String>(); was required prior to JDK 7. In JDK 7 they introduced the 'diamond operator', where the type is inferred.

So, if you are using JDK 7+ you can simply use new Arraylist<>()

You can see this in the Java Language Specification under Class Instance Creation Expressions, or search for 'Diamond Operator'

vman
  • 1,214
  • 11
  • 20
0

In java 7 and later there is no difference. The latter is considered more concise.
The so called diamond operator <> is helping here for the type to be inferred.

George
  • 6,706
  • 8
  • 30
  • 42
0

This is a 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.

http://www.javaworld.com/article/2074080/core-java/jdk-7--the-diamond-operator.html

Oleksandr Loushkin
  • 1,399
  • 11
  • 21