Java 7 : Languages updates from Project Coin

I continue my posts on the new features of Java 7. In this post, I'll detail the news coming from the Project Coin. This project has computed more than 70 new features proposal from the Java community for integration in Java 7.

In this post, I'll detail the Final Five (or So) features chosen from this project to be included in Java 7.

Diamond Syntax

This feature is a really simple syntax improvement for generics. With that feature, you can avoid to write twice the generics type in a generics declaration. A little example :

Instead of writing this :

Map<String, Collection<Integer>> map = new LinkedHashMap<String, Collection<Integer>>();

You can now write that :

Map<String, Collection<Integer>> map = new LinkedHashMap<>();

That's really practical, but not essential.

Simplified Varargs Method Invocation

This improvement is not a new functionality, but only a move of a warning. Like you must know, we cannot create arrray of generics type because the type verification is not made at the same time. But with the Ellipse of Java 5, you can made that type of array implicitely and the compiler generate warning at each invocation of that kind of method, so the warning has moved to the method declaration to have less warnings.

Integers declaration

You'll can declare integers using binary values :

int binary = 0b11001001001;

and you can use _ (underscores) in the declaration :

double amount = 1_999_888_777.25;
int color = 0xdd_dd_dd;
int binary = 0b110_0100_1001;

That's allow to make more verbose code, but it's only sugar.

Collections manipulations and declaration

Another improvements to code verbosity, is the support of collections in the language. You'll have code facility to access and edit indexed collections like list and maps and to declare easily collections.

First, you can access to an element using the same syntax as the array :

List<String> list = ...;
Map<String, String> map = ...;
String firstValue = list[0];
map["Test"] = firstValue;
String valueFromMap = map["Test"];

For the maps, that works with any type of key. So if you have one of your class for key, you can directly pass it in the code like the Strings in my example.

And, you can also quickly declare collections like array :

Lists with [] :

List<Integer> numbers = [ 1, 2, 4, 8, 16, 32, 64, 128 ];

Sets with {}

Set<Integer> numbers = { 256, 512, 1024, 2048, 4096 };

And Maps with {} and : to split value and key :

Map<String, String> translations = {
  "Hi" : "Bonjour",
  "Goodbye" : "Au revoir",
  "Thanks" : "Merci"

All that created collections are immutables.

Strings switch

A really good feature : Switch with Strings values. You can now do that kind of switch : ¨

String value = "";

switch (language) {
  case "fr":
    value = "Bonjour";
  case "en":
    value = "Hi";
  case "de":
    value = "Guten tag";
    value = "Hello";

I thinks, it's really great. With that, we can delete a lot of ugly list of if/else if code.

Automatic Resource Management (ARM)

Another great feature, you can automatically close the resources using a new try clause :

public void write(URL url, File file) throws IOException {
  try ( FileOutputStream fos = new FileOutputStream(file); InputStream is = url.openStream() ) {
    byte[] buf = new byte[2048];
    int len;
    while ((len = &amp;gt; 0) {
      fos.write(buf, 0, len);

In that code, the FileOutputStream and the InputStream will automatically be closed after the try, making a cleared code and you cannot forgot a resource with that.

And last, the modifications to support the JSR 292 directly in the language. I've already described that features in other post : Java 7 : More Dynamics

That's all for these new language enhancements from the Project Coin.

For me, i like the new ARM and the Strings Switch, but i think this is simple enhancements, they were others proposals in the Project Coin i found better, like the "Improved Exception Handling for Java", "Improved Wildcard Syntax for Java" or "Elvis and Other Null-Safe Operators", but this a good start.

Related articles

  • Java 7 has been released!
  • Java 7 : Add "public defender methods" to Java interfaces
  • Java File Copy Benchmark Updates (once again)
  • Java 7 : The new try-with-resources statement
  • Better exception handling in Java 7 : Multicatch and final rethrow
  • JDK 7 Features updated ! Plan B has apparently been approved
  • Comments

    Comments powered by Disqus