JavaJava 7 Language Changes: Making Sense of Project Coin

Java 7 Language Changes: Making Sense of Project Coin

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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.

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.

Java 7 Language Change #3: Try with Resource Statement

Prior to the release of JDK 7, Java programmers had to manually releasing the resources (such as database connections, file handlers, and so on) that they were using in the code segment. Many times it becomes tedious to manage these, and even more difficult when exceptions occur during the process. Analyzing/debugging such scenarios is a nightmare that developers still fear.

The following code is a classic example of how to use try with catch for a file I/O operation.

Listing 3: FileIO with try
import java.io.*; class FileIO { int readValue; FileInputStream fileInputStream; public static void main(String args[]) { FileIO fileIO = new FileIO(); fileIO.readFile(); } private void readFile() { try{ fileInputStream = new FileInputStream("FileIO.java"); while((readValue = fileInputStream.read()) != -1) System.out.print((char)readValue); }catch(IOException ioexception) { System.out.println("IOException occurred: "+ioexception.getMessage()); try{ if (fileInputStream != null) { fileInputStream.close(); } }catch(IOException ioe) { //do nothing } } } }

The FileInputStream instance is closed in the catch block. Please note the corresponding try/catch that is present in the catch block as well. In general this will not result in the stream closing if there is an exception and to resources being held up forever.

Listing 4 shows an improved version where the priority is given to the resources that are declared as part of the try block, and all exceptions (if any) are lowered in priority.

Listing 4: FileIO with try-with-resource
import java.io.*; class FileIO { int readValue; FileInputStream fileInputStream; public static void main(String args[]) { FileIO fileIO = new FileIO(); fileIO.readFile(); } private void readFile() { try(FileInputStream fileInputStream = new FileInputStream("FileIO.java")) { while((readValue = fileInputStream.read()) != -1) System.out.print((char)readValue); }catch(IOException ioexception) { //Only handle what is needed. Do not worry about resources System.out.println("IOException occurred: "+ioexception.getMessage()); } } }

Benefit from Project Coin

These Java language changes, although small in nature, deliver big improvements in code complexity and maintenance. As always, it will take time to get familiar with them but they address some pain points the Java community has faced for years.

Sridhar M S is a Java developer from Bangalore, India. He holds a master’s degree in Computer Science.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories