Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Project Coin Examples with JDK 7 Developer Preview

DZone's Guide to

Project Coin Examples with JDK 7 Developer Preview

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}