Friday, April 13, 2012

Java 7: Diamond Operator in Generics


Java 7 promotes the concept of convention over configuration. It simplifies the use of generics by using the diamond operator(<>) for constructor type inference. When using the diamond operator, the compiler infers the parameter types for the constructors which avoids explicit specification of the object. Before Java7:
List list = new ArrayList();
With Java 7, the diamond operator makes this shorter and simpler. It avoids unchecked warnings in a program and also reduces generic verbosity.
List list = new ArrayList<>();
In above case, compiler automatically infers 'String' for the constructor of ArrayList.

1 comment:

Jacob said...

It was very interesting for me to read that blog. Thanks the author for it. I like such topics and everything that is connected to them. I would like to read more soon.
1988 Mercury Merkur Scorpio AC Compressor