Menu

Nakov.com logo

Thoughts on Software Engineering

Implicit Variables in Java 7?

Few days ago the Belgian Java Users Group (BeJUG) and the Brazilian Java Users Group (SouJava) jointly discussed a submission of few new features as language JSR for JDK7 in the Java Community Process. Lot of small nice features were introduced but one thins was still missing.

Implicit Variable Declarations

The implicit variable declarations with “var” keyword (like in C# 3.0) can significantly reduce the ammount of code developers write every day. For example the code below:

Hashtable<String, Integer> hashtable = new Hashtable<String, Integer>();
Set<Map.Entry<String,Integer>> entries = hashtable.entrySet();

could be simplified to:

Hashtable<String, Integer> hashtable = new Hashtable<String, Integer>();
var entries = hashtable.entrySet();

We could use even more interesting constructs like this:

Hashtable<String, Integer> hashtable = new Hashtable<String, Integer>();
for (var entry : hashtable.entrySet()) {
System.out.printf("%s --> %s\n", entry.getKey(), entry.getValue());
}

The type of the “entires” and “entry” variables is implicitly defined from the right side of the expressions in the above code so we don’t want to write the boring and long type definition.

Nice! I am very happy to see this in C# 3.0. Why not in Java 7 too? What do you think?

Comments (1)

One Response to “Implicit Variables in Java 7?”

  1. over at this website

    Implicit Variables in Java 7? | Svetlin Nakov’s Blog

RSS feed for comments on this post. TrackBack URL

LEAVE A COMMENT