Nick has been a passionate Java developer for 10 years now. In his day job he is working as an enterprise Java web developer. In his spare time he likes to learn new programming languages and new Java related technologies, and he regularly posts articles on his blog. Nick is a DZone MVB and is not an employee of DZone and has posted 7 posts at DZone. View Full User Profile

Project Coin Examples with JDK 7 Developer Preview

02.25.2011
| 6516 views |
  • submit to reddit

Mark Reinhold announced availability of the first JDK 7 developer preview today. Reason enough for me to give some of the Project Coin features a shot. I won't get into details here, but instead just show some quick examples. Most of them are self-explanatory anyway.

Code is also available via github.

Strings in switch

final String str = "foo";

switch (str) {
case "foo":
System.out.println("mooh");
break;
case "bar":
System.out.println("miau");
break;
default:
break;
}

Binary integral literals and underscores in numeric literals

final int large = 1_000_000;
System.out.println(large);

final int binary = 0b1011;
System.out.println(binary);

Multi-catch and more precise rethrow

 

class ExA extends Exception {}
class ExB extends Exception {}

public void testMultiCatch() {

final int a = 0;

try {
if (a == 0) {
throw new ExA();
}
if (a == 1) {
throw new ExB();
}
} catch (ExA|ExB ex) {
System.out.println(ex.getClass() +
" was thrown and caught");
}

}

public void testReThrow() throws ExB {

final int a = 0;

try {
if (a == 0) {
throw new ExA();
}
if (a == 1) {
throw new ExB();
}
} catch (final ExA exa) {
System.out.println("Exa was caught");
} catch (final Exception ex) {
System.out.println(ex.getClass() +
" was thrown, caught and rethrown");
throw ex;
}

}

Improved type inference for generic instance creation (diamond)

final List<String> list = new ArrayList<>();
list.add("Foo");

System.out.println(list);

try-with-resources statement

try (final BufferedReader br = new BufferedReader(new FileReader("./TestAutomaticResourceManagement.java"))) {

String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}

} catch (final IOException e) {
}

From http://stronglytypedblog.blogspot.com/2011/02/project-coin-examples-with-jdk-7.html

Published at DZone with permission of Nick Wiedenbrueck, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Martijn Verburg replied on Fri, 2011/02/25 - 7:22am

Hi Nick, great to see the code samples! We've put up an announcement with the install instructions for getting the preview working with Netbeans at http://java.dzone.com/announcements/get-started-jdk-7-and-netbeans hope it helps people get these code samples running! I've already found using the _ character in my banking application to be a major improvement for readability :). Cheers, Martijn (@karianna, @java7developer)

Lars Vogel replied on Tue, 2011/03/01 - 4:52pm

I think an example of closable would also be nice.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.