New Java 7 Language Features
Last week I was at the Java 7 launch party in Sao Paulo, Brazil and it was really cool. There were a lot of developers on the party to celebrate Java 7 (afterall, we have been waiting for it for 5 years!).
I took some pictures at the event: http://www.flickr.com/photos/loiane/sets/72157627015952961/
Video of the event: http://blogs.oracle.com/java/entry/java_7_celebration_begins
Slides: http://blogs.oracle.com/darcy/resource/ProjectCoin/CoinLaunch.pdf
Anyways, I was really excited with the event and I went home and started to play with Java 7 language features.
As I will have to wait for Open JDK for Mac to be released, I had to play with it on Windows. You can download Java 7 here.
Another issue I found is Eclipse does not have native support for Java 7. They released a plugin, but it still beta. So I decided to play with NetBeans.
The current version of NetBeans is 7, and it already comes with native support to Java 7. If you install Java 7 and then install NetBeans, it will set Java 7 as the default JDK for development.
Then I created a Java Project on NetBeans but the source code was still compiling in Java 6. You have to make a small change to make it compile with Java 7.
All set and I started to play with some languages new features, aka Project Coin.
Strings in Switch:
In all examples, I tried first to write code as we usually do, and then convert the code to a Java 7 feature. The cool thing about NetBeans is it already have tips to convert the code.
First, will compare some Strings using if-else. NetBeans will popup a warning like this:
And if we accept the change, it will convert the code into this:
Following is the code:
Before:
public void testStringInSwitch(String param){
final String JAVA5 = "Java 5";
final String JAVA6 = "Java 6";
final String JAVA7 = "Java 7";
if (param.equals(JAVA5)){
System.out.println(JAVA5);
} else if (param.equals(JAVA6)){
System.out.println(JAVA6);
} else if (param.equals(JAVA7)){
System.out.println(JAVA7);
}
}
Java7:
public void testStringInSwitch(String param){
final String JAVA5 = "Java 5";
final String JAVA6 = "Java 6";
final String JAVA7 = "Java 7";
switch (param) {
case JAVA5:
System.out.println(JAVA5);
break;
case JAVA6:
System.out.println(JAVA6);
break;
case JAVA7:
System.out.println(JAVA7);
break;
}
}
Binary Literals:
Before:
public void testBinaryIntegralLiterals(){
int binary = 8;
if (binary == 8){
System.out.println(true);
} else{
System.out.println(false);
}
}
Java7:
public void testBinaryIntegralLiterals(){
int binary = 0b1000; //2^3 = 8
if (binary == 8){
System.out.println(true);
} else{
System.out.println(false);
}
}
And if you try to declare a constant number in the code, NetBeans will warn you to convert it into a Binary Literal:
to
public void testUnderscoresNumericLiterals() {
int oneMillion_ = 1_000_000;
int oneMillion = 1000000;
if (oneMillion_ == oneMillion){
System.out.println(true);
} else{
System.out.println(false);
}
}
Underscore Between Literals:
public void testUnderscoresNumericLiterals() {
int oneMillion_ = 1_000_000; //new
int oneMillion = 1000000;
if (oneMillion_ == oneMillion){
System.out.println(true);
} else{
System.out.println(false);
}
}
Diamond Syntax:
Before:
public void testDinamond(){
List<String> list = new ArrayList<String>();
Map<String, List<String>> map = new HashMap<String, List<String>>();
}
Java7:
public void testDinamond(){
List<String> list = new ArrayList<>();
Map<String, List<String>> map = new HashMap<>();
}
When we write code in Java 6 syntax, NetBeans will warn us to convert it into Java 7 Diamond Syntax:
To
Multi-Catch Similar Exceptions:
Before:
public void testMultiCatch(){
try {
throw new FileNotFoundException("FileNotFoundException");
} catch (FileNotFoundException fnfo) {
fnfo.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Java 7:
public void testMultiCatch(){
try {
throw new FileNotFoundException("FileNotFoundException");
} catch (FileNotFoundException | IOException fnfo) {
fnfo.printStackTrace();
}
}
Same thing here. NetBeans will ask to convert the code into Java 7:
To
Try with Resources:
Before:
public void testTryWithResourcesStatement() throws FileNotFoundException, IOException{
FileInputStream in = null;
try {
in = new FileInputStream("java7.txt");
System.out.println(in.read());
} finally {
if (in != null) {
in.close();
}
}
}
Java 7:
public void testTryWithResourcesStatement() throws FileNotFoundException, IOException{
try (FileInputStream in = new FileInputStream("java7.txt")) {
System.out.println(in.read());
}
}
Same thing here:
To:
There is also varargs simplification, but I did not understand this one very well to code an example.
There are another features that were submited, but did not make it into this release:
The code presented on this post is not only theory, I really ran it in my machine. You can get/download the NetBeans project from my github: https://github.com/loiane/Java7HelloWorld
A quick update: A DZone released today a RefCardz about NetBeans and Java 7 – it is a pdf and the download is free: http://refcardz.dzone.com/refcardz/netbeans-ide-7-programming












In the Diamond Syntax section, you seem to have run into some HTML issues. The opening diamond bracket isn’t showing up in either example.
Also on the subject, aren’t null-safe navigation and the elvis operator the same thing? Closures were removed from Java 7 and moved to the next release (maybe) as well.
Null-safe navigation seems like it would have been a much more worthwhile addition than binary literals or even underscored literals. Most people who have to deal with binary data are generally comfortable working in hex and how often does anyone have to deal with enormous literals? In contrast, how often do NPEs pop up?
Thanks Alan,
Fixed it.
Yes, I agree with you. The diamond syntax was a good addition because it is a little bit annoying to duplicate the generic type.
The Null-safe feature would be the best adding for developers. Missed this one.
We can add here the new Java IO which is a significant improvement to the old fashioned IO since Java 1.2 version
Really good! Underscore between literals… it’s new for me.
Loiane…. Cool stuff !!! I enjoyed it !!!
thanks,
Nice. tutorial…
This is really helpful. Clearly mentioned all the new features in java 7. Thanx a lot…