The Wayback Machine - https://web.archive.org/web/20111205035929/http://www.developer.com:80/java/java-7-language-changes-making-sense-of-project-coin.html
December 4, 2011
RSS RSS feed

Java 7 Language Changes: Making Sense of Project Coin

With JDK 7 taking shape, a lot of groundwork has taken place in the Java development community to agree upon essential improvements that will be included in version 7 of the Java SE platform.

One notable set of language changes is being ushered in by Project Coin, a project that determines small language changes to add to JDK 7. Shaped by the numerous proposals and feedback from the community, Project Coin is making the following changes/enhancements:

  • Strings in switch
  • Multi-catch and re-throwing precisely
  • Try with resource statement
  • Improved type interface for generic instance creation
  • Simplified varargs method invocation

In this article, I explain how three of these Project Coin enhancements could have a major impact on the overall Java language. I will cover the rest in an upcoming article. Note that these changes are effective only in JDK 7. Please download JDK 7 and ensure the necessary settings are made before trying any code in this article.


Click here to download

 

Java 7 Language Change #1: Strings in Switch

Strings are constants too yet prior to this Project Coin proposal only constants could be used as arguments in switch statements and strings were not allowed to be part of switch statements. By allowing strings in switch statements, strings can be in the argument list for switch statements, which also implies that strings are constants.

The details of how strings are represented in Java are beyond the scope of this discussion. Suffice it to say that strings are constants and they can be used as any other allowed argument type in switch statements.

To understand this change better, consider the following listing.

Listing 1: Using String(s) in Switch Statement
class StringsInSwitch { public static void main(String args[]) { StringsInSwitch stringsInSwitch = new StringsInSwitch(); stringsInSwitch.processRequest(args[0]); //Assuming that user enters an argument at the time of execution } private void processRequest(String strArg) { switch(strArg) { case "Hello": System.out.println("Hi"); break; case "Bye": System.out.println("Bye"); break; case default: System.out.println("Understand only Hello and Bye. Please re-type"); break; } } }

Java 7 Language Change #2: Multi-catch and Re-throwing Precisely

Prior to JDK 7, whenever Java programmers needed to catch multiple exceptions and handle them in the same way, they would either repeat the code in the catch blocks or use the finally block. Doing this task in the finally block would still not work in cases when an exception occurring the finally block or the code will be executed in both the cases of running into an exception or not, which may not be an ideal requirement.

Listing 2: Using Multiple Catches
class MultiCatch { int a=10; public static void main(String args[]) { MultiCatch multiCatch = new MultiCatch(); multiCatch.compute(args[0]); } private void compute(String arg) { try{ int b = Integer.parseInt(arg); System.out.println(a/b); }catch (ArithmeticException | NullPointerException exception) { System.out.println("Exception occurred: "+exception.getMessage()); } } }

Of course the whole point here is to illustrate the usage or how to use multiple catches in the catch block. So, don't pay too much attention to the syntax of the code. You can make the code suit your specific requirements by including the respective business logic.


Tags: Java 7



Networking Solutions
Sitemap | Contact Us