Java 1.7 new Language Features and Enhancements

After long time I came back to my blog page and decided to continue filling the space in my blog.

In this blog I'm going to write about the new bunch of features coming with all new JAVA 1.7 or JDK 1.7.
Once I gone through those features I really realize some of those enhancements are the things that we are waiting until Sun include those features for us.

The following enhancements have been added to the Java language:

  • Binary Literals
  • Underscores in Numeric Literals
  • Strings in switch Statements
  • Type Inference for Generic Instance Creation
  • Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods
  • The try-with-resources Statement
  • Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking

Binary Literals
    In Java SE 1.7 it includes the binary number system with the integral types (byte, short, int, long). To specify 
number as a binary value it has to add 0b or 0B as a prefix. following is the example.
  // An 8-bit 'byte' value:
byte aByte = (byte)0b00100001;
// A 16-bit 'short' value:
short aShort = (short)0b1010000101000101;

// Some 32-bit 'int' values:
int anInt1 = 0b10100001010001011010000101000101;
Underscores in Numeric Literals
 In Java 1.7 they have introduce "_" to apply between anywhere in numeric values to increase readability.
 Following example shows the ways that we can apply this feature.

long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi =  3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
Strings in switch Statements
Another key enhancement that would waiting for long time is this feature. In earlier editions "switch" 
support only Enums or Compile time constant integer values. but now it takes the strings which associates
with case literals. Using this feature we can avoid lots of if-else-if using in a code and java compiler generates more efficient byte code from switch than if-else-if.

public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
     String typeOfDay;
     switch (dayOfWeekArg) {
         case "Monday":
             typeOfDay = "Start of work week";
         case "Tuesday":
         case "Wednesday": 
  typeOfDay = "mid of the week";
             throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
     return typeOfDay;
Type Inference for Generic Instance Creation
    This feature enable us to specify the generics with out  its generic class type in constructor side. Compiler will infer the type argument from the context. This feature seems bit confusing but it reduce the amount of lines.

For example, consider the following variable declaration:
Map<String, List<String>> myMap = new HashMap<String, List<String>>();
In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (<>):
Map<String, List<String>> myMap = new HashMap<>();

There are more features coming with new Java SE 1.7 you can check those out from here


Popular posts from this blog

PostgreSQL bytea and oid

MySQL as Hive metadata store

Big Memory Go.