What’s new in Java 7- Features as part of Project Coin

As part of the Project Coin, there were quite a few language¬†enhancements were added to the Language. The last time Java language changed was in the Java 5 release when Generics were added. The changes in introduced in the Java 7 were welcomed by java community around the world. This was seen as a stepping stone to the plethora of changes to be introduced with Java 8 and above. Nonetheless Java 7 has its own set of issues, but this shouldn’t be a concern for someone to download the latest build and give it a shot.

also read:

In this post I would like to pick a few features added as part of Project Coin and in the coming posts try to cover the remaining features. The enhancements we would be looking at are:

  • String in Switch statements.
  • Generic Type Inference (Diamond Operator)
  • Multi Catch blocks
  • Binary Integral literals and underscores in numeric literals.

Generic Type Inference (Diamond Operator)
Pre Java 7, for a map of string versus list of integers we would have a declaration like:

 
Map<Stirng,List<Integer>> myMap = new HashMap<Stirng,List<Integer>>();

we could also write:

 
Map<Stirng,List<Integer>> myMap = new HashMap();

But for the above code the compiler would issue an warning:

Note: Java7FeatureDemo.java uses unchecked or unsafe operations.

With Java 7, the above declaration is equivalent to:

 
Map<Stirng,List<Integer>> myMap = new HashMap<>();

The compiler now would inference the type based on the declaration on the left. This is in my opinion is a good step towards removing redundancy in the declaration.

If you are using a IDE which supports Java 7 language enhancements, in my case I was using IntelliJ, when you use the pre Java 7 type of declaration it suggests:

Java 7 - Diamond Operator

Strings in switch:
Prior to Java 7, we didn’t have an option to use Strings in switch. An alternative approach was to use enums in the switch statement:

 
public class PreJava7 {
  enum GRADE{
    A,B,C,D;
  }

  public static void main(String[] args){

    GRADE choice = GRADE.A;

    switch (choice){
      case A:
        System.out.println("A chosen");
        break;
      case B:
        System.out.println("B chosen");
        break;
      case C:
        System.out.println("C chosen");
        break;
    }
  }
}

With Java 7 the same code can be written as:

 
String choice = "A";

switch (choice){
  case "A":
    System.out.println("A chosen");
    break;
  case "B":
    System.out.println("B chosen");
    break;
  case "C":
    System.out.println("C chosen");
    break;
}

Multi Catch blocks:
If a code block throws multiple exceptions and you want all of the catch blocks to do the same operation, say logging the exception, we would write something like:

 
public class PreJava7 {
  public static void main(String[] args){
    try {
      exceptionMethod1();
      exceptionMethod2();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (NoSuchFieldException e) {
      e.printStackTrace();
    }

  }
  static void exceptionMethod1() throws IOException{}
  static void exceptionMethod2() throws NoSuchFieldException{}
}

Look at the redundancy of code in both the catch blocks. With the introduction of Mutli catch blocks, the same catch block can be used to capture multiple exceptions thereby leading to code reuse. So the modified code would be:

 
try {
  exceptionMethod1();
  exceptionMethod2();
} catch (IOException | NoSuchFieldException e) {
  e.printStackTrace();
}

If you use an IDE which supports Java 7 features, then using multiple catch blocks would generate the suggestion as follows:
Java 7 Multiple Catch Blocks

Binary Integral literals and underscores in numeric literals:
Binary literals can be represented by using b or B, just like the way we used x for Hexadecimal literals.

 
int binary1 = 0b011;
int binary2 = 0B111;

System.out.println(binary1);
System.out.println(binary2);

Now coming to the underscores in numeric literals:
Pre Java 7, we would write something like:

 
int hugeNumber = 1000000000;

A trivia now: How fast can you count the number of zeros in hugeNumber? The time taken to count the number increases with the age of a person. Jokes apart, lets see how this is can be changed with Java 7 enhancement:

 
int hugeNumber2 = 1_000_000_000;
System.out.println(hugeNumber == hugeNumber2);//Prints True

So, how easy is it now to count the number of zeros?

These were some of the few enhancements added as part of Project Coin in Java 7 release. There are few more features which were pushed to Java 8 and another feature try-with-resources ( Automatic Resource Management) which is part of Java 7 and I would write about it in my upcoming posts.

If you are interested in obtaining a fully functional code, please do it from here.
Note: In order to run the examples given above you need to download the JDK 7 from here.

Comments

comments

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Comments

  1. Crisp and clear article abt the some of JavaSE7 features

  2. Deepak A L says:

    Could you compile the new features of Java 7 with working code snippets in a pdf and make it available for download..Please reply

Trackbacks

  1. [...] What’s new in Java 7- Features as part of Project Coin [...]

  2. [...] happening in the Java community. You must have seen plethora of articles on try-with-resources, strings in switch statements and other minor language enhancements as part of Project Coin. These are just few of the [...]

  3. JavaPins says:

    What’s new in Java 7- Features as part of Project Coin…

    Thank you for submitting this cool story – Trackback from JavaPins…

Speak Your Mind

*