Java 7 Features

1. Strings in Switch:

public void testStringInSwitch(String param){
       final String JAVA5 = “Java 5”;
       final String JAVA6 = “Java 6”;
       final String JAVA7 = “Java 7”;
       switch (param) {
           case JAVA5:
               System.out.println(JAVA5);
               break;
           case JAVA6:
               System.out.println(JAVA6);
               break;
           case JAVA7:
               System.out.println(JAVA7);
               break;
       }
   }
 
2. Binary Literals:

public void testBinaryIntegralLiterals(){
        int binary = 0b1000; //2^3 = 8
        if (binary == 8){
            System.out.println(true);
        } else{
            System.out.println(false);
        }
}

3. Underscore Between Literals:

public void testUnderscoresNumericLiterals() {
    int oneMillion_ = 1_000_000; //new
    int oneMillion = 1000000;
    if (oneMillion_ == oneMillion){
        System.out.println(true);
    } else{
        System.out.println(false);
    }
}

4. Diamond Syntax:

public void testDinamond(){
    List list = new ArrayList<>();
    Map> map = new HashMap<>();
}



5. Multi-Catch Similar Exceptions:

public void testMultiCatch(){
    try {
        throw new FileNotFoundException(“FileNotFoundException”);
    } catch (FileNotFoundException | IOException fnfo) {
        fnfo.printStackTrace();
    }
}

6. Try with Resources:

public void testTryWithResourcesStatement() throws FileNotFoundException, IOException{
    try (FileInputStream in = new FileInputStream(“java7.txt”)) {
        System.out.println(in.read());
    }
}

Write a Reply or Comment

Your email address will not be published.