0

I was recently porting a Java 7 project down to Java 6 and I a had some Arraylists being initialized with empty type parameters.

Of course it gave quite a few errors because of it only being allowed in Java 7

So then I was wondering; What use do I get from using empty type parameters?

ex.: new Arraylist <>();

  • That means the parameter type can be resolved from context. It's just eye candy so that it's faster to write. – MightyPork Jul 12 '14 at 09:12

2 Answers2

0

It is called generics. It is a compile time concept and is used for type safety. You can take a look about generics in documentation.

If you use

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

i.e. with no generics ON the RHS then it is similar as saying

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

This is just a feature added in Java 7. As stated previously this being a compile time concept the generics of reference variable is what matters and not of actual object (created with new/RHS)

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

It's called diamond operator and infers the generic type from the left hand side, if possible. Saves you a few keystrokes and was introduced with Java 7 (project coin).

atamanroman
  • 11,607
  • 7
  • 57
  • 81