Java 7 – New Language Features

By akanksha
Aug 6th, 2011

The world of IT industry is already overloaded with Java and its related technology and now we have Java7 to buzz our world again with its new and interesting features. JDK 7 introduces several key features to improve performance, usability, and security of the Java platform. Java 7 is already in market from July 28th 2011. It includes new language features, a new bytecode for dynamic languages, a real filesystem API, and a fork/join framework for writing scalable parallel programs. Main features of Java7 under its language features are:

Binary Literals
Java7 allows for its integral type (byte, short, int, long) to be represented in binary form by using 0b or 0B as prefix.
int binaryInteger = 0b100010
Underscore in Numeric Literals
In order to increase readability of your code, Java7 allows any number of underscore characters (_) anywhere between the digits in numeric literal.
int sampleInteger = 145_098_040
Strings in switch statement
Until java 6 u can use only numbers and enums in switch statement but Java7 allows string to be used in switch expression. The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String.equals method; consequently, the comparison of String objects in switch statements is case sensitive. The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.
String day = "Sunday";
switch (day)
    case "Monday":


Automatic Resource management with try-with resources statement
Some resources in java need to be closed manually like inputstream, reader, writer, sockets. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement, can be used as a resource.

try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path))) {
    return bufferedReader.readLine();

Here BufferedReader will be closed irrespective of the fact whether try statement executes normally or abruptly. So this avoids the need of having ‘finally’ block to close the resources.

Catching multiple exception
This feature allow single catch {} block to handle multiple exceptions, thereby reducing code duplication.

catch (IOException|SQLException exe) {
    throw exe;

Bytecode generated by compiling a catch block that handles multiple exception types will be smaller (and thus superior) than compiling many catch blocks that handle only one exception type each. A catch block that handles multiple exception types creates no duplication in the bytecode generated by the compiler; the bytecode has no replication of exception handlers.

Improve Type Inference for Generic Instance Creation
You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (<>) as long as the compiler can infer the type arguments from the context. Putting it simply :

Map<<String>,<List<String>>  sampleMap = new TreeMap<<String>,<List<String>>();

can now be simply put as:

Map<<String>,<List<String>>  sampleMap = new TreeMap<>();

Improved compiler warnings and errors when used with non-refiable formal parameters in varargs

The Java SE 7 complier generates a warning at the declaration site of a varargs method or constructor with a non- refiable varargs formal parameter. Java SE 7 introduces the compiler option -Xlint:varargs and the annotations @SafeVarargs and @SuppressWarnings({“unchecked”, “varargs”}) to suppress these warnings.
Since this needs more explanation u can refer to .

About the Author

- Akanksha Gupta is a Software Developer by Profession, Technical Writer by Passion and a Poet by Nature. Her domain of technical specialization is Java. You can read her poems at Zindagi Meri Zubani .

2 Responses to “Java 7 – New Language Features”

  1. High Akanksha , Plz give some tutorial on getting a Job outside the country for freshers .

Leave a 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.

facebook comments: