Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Project Coin: "Small" Java 7 Tweaks, Big Dev Gains

The smaller Java 7 enhancements, which are at the heart of Project Coin, make the Java language much simpler for developers.


advertisement

There are several new features being introduced with the new release of JDK 7. Some of the important changes that will be introduced in Java 7 include:

  1. Project Coin
  2. Join/Fork Framework
  3. File System NIO2
  4. Changes to the JVM

We will cover all these features in our series on New Java 7 Features. In this first installment, we explore the features of Project Coin.

What Is Project Coin?

Project Coin (JSR 334) features some of the smaller enhancements made to JDK 7. The objective of this project is to make programs more readable and reliable, as well as to make them integrate well with existing and future versions of Java. Project Coin features have been introduced primarily at the language level.



The following features are supported as part of Project Coin:

  1. Using strings in switch statements
  2. Improved generic instance creation
  3. Binary integral literals and usage of underscores in numeric literals
  4. Enhancements in exception handling mechanisms:
    • Optimized rethrow
    • Multi-catch
    • Try-with-resources
  5. Simplified varargs method invocation

In this article, we drill down on all these enhancements and provide some code snippets.

Project Coin: Using Strings in Switch Statements

Switch statements are one of the primary constructs of any programming language. In all the earlier versions of Java, the valid data type for case could be one of int, short, char or byte constants. Earlier versions of Java also supported enum as one of the types for case. With the release of Java 7, the much needed string constant can be used as a value for case. The strings are matched with the appropriate cases using the equals method of the String class.

The following code snippet demonstrates the use of strings in a switch statement:

String role = "Programmer";
Double bonus;
switch (role) {
case "Manager":
         bonus = 10000.0;
            break;
     case "Architect":
         bonus = 80000.0;
            break;
      case "Programmer":
         bonus = 5000.0;
          break;
      default:
           bonus = 0.0;
          break;
}

Project Coin: Improved Generic Instance Creation

Generics was one of the major concepts/new features introduced in Java 5 for achieving compile time safety. While creating an object of generic type, the language mandated both the reference type and actual type to define the generic syntax.

Until Java 7, generic instances were created like this:

Map<Integer, String> myNewMap = new HashMap<Integer, String>();

Java 7 allows the freedom to use only the generic syntax for the reference. In this revised version of the above code snippet, the myNewMap reference type is created without specifying the type information for the actual type.

Map<Integer, String> myNewMap = new HashMap<>();

Project Coin: Binary Integral Literals, Underscores in Numeric Literals

In Java 7, a numeric constant can store a binary value. The binary value helps developers who are working on low-level APIs to deal with byte code to manipulate the numeric values. In this example, the binary equivalent value of 102 is stored for the int variable number.

int number = 0b1100110;

The other change being made to the numeric literal is the addition of underscores. Lengthy numeric values can be separated using underscores and they remain valid as long as the underscore is used between valid numerical values. The idea behind this addition is to make the numbers easier for users to read and interpret.

Here is an example:

long longNum = 27_04_86L;
long ccnumber = 3434_5656_3903_3444L;

The ccnumber in the snippet above is more readable and it is represented as 3434565639033444L in the system.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap