Java Features by Version- Java 7

Java 7 Features(2011)

Underscores in Numeric Literals (Java 7)

Java allows you to use underscore in numeric literals.

  • You cannot use underscore at the beginning or end of a number.

  • You cannot use underscore adjacent to a decimal point in a floating point literal.( 10._0)

  • You cannot use underscore prior to an F or L suffix (long a = 10_100_00_L;)

  • added in Java 7. You can have underscores in numbers to make them easier to read: ```java int million1 = 1000000; int million2 = 1_000_000;

double notAtStart = 1000.00; // DOES NOT COMPILE double notAtEnd = 1000.00; // DOES NOT COMPILE double notByDecimal = 1000_.00; // DOES NOT COMPILE


<br>

# String in switch statement (Java 7)
```java
public class StringInSwitchStatementExample {  
    public static void main(String[] args) {  
        String game = "Cricket";  
        switch(game){  
        case "Hockey":  
            System.out.println("Let's play Hockey");  
            break;  
        case "Cricket":  
            System.out.println("Let's play Cricket");  
            break;  
        case "Football":  
            System.out.println("Let's play Football");  
        }  
    }  
}


The try-with-resources (Java 7)

the try-with-resources statement is a try statement that declares one or more resources. The resource is as an object that must be closed after finishing the program. The try-with-resources statement ensures that each resource is closed at the end of the statement execution.

You can pass any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable

Caching Multiple Exceptions by single catch (Java 7)

Java allows you to catch multiple type exceptions in a single catch block. It was introduced in Java 7 and helps to optimize code.You can use vertical bar (|) to separate multiple exceptions in catch block.

Diamond operator <>, removes right side of generics

you can replace the type arguments with an empty set of type parameters (<>). This pair of angle brackets is informally called the diamond.

The following approach is used in Java 6 and prior version.

Ex. List<Integer> list  = new List<Integer>();  

Now, you can use the following new approach introduced in Java 7.
Ex. List<Integer> list = new List<>(); // Here, we just used diamond