• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

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

May 2, 2012 //  by Mohamed Sanaulla//  Leave a Comment

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:

  • Java 7.0 Tutorials
  • New Features in Java 7.0
  • G1 Garbage Collector in Java 7.0

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.

Category: JavaTag: Java, java 7, project coin

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.

Previous Post: « Invoking RESTful Web Service using API in java.net and GSON
Next Post: Using JsonSlurper Groovy API from Java to parse JSON »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact