Java 1.7 Features

Language Enhancements::

  1. Integral types (byte, short, int, and long) can also be expressed using the binary number system.
    byte sampleByte = (byte)0b01001101;

  2. String values can be used in Switch statements. Internally switch uses String.equals() for finalizing which case to be executed and the comparison is a case sensitive.

  3. try-with-resources is new option through which we can declare resources(objects which implements AutoCloseable) with in try statement and the same resource can be closed by JVM automatically.
    try (BufferedReader br = new BufferedReader(new FileReader(path)))
    {
        return br.readLine();
    }

    Note: A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. Any catch or finally block is run after the resources declared have been closed.

  4. Catching Multiple Exceptions in same catch block.
    catch (IOException|SQLException ex)
    {
        logger.log(ex);
        throw ex;
    }

  5. Re-throwing Exceptions with more inclusive type checking.

  6. We can use underscore anywhere in between numerical literals.
    long longNum = 12_34_56_789L  //valid
    float pi = 3.1_4F  //valid
    long longNum = _1234567L  //invalid
    long longNum = 1234567L_  //invalid
    long longNum = 1234567_L  //invalid
    float pi = 3_.1415F  //invalid
    float pi = 3._1415F  //invalid
    float pi = 3.1415_F  //invalid

  7. 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. This pair of angle brackets is informally called the diamond operator.
    Map<Integer,Integer> sampleHashMap = new HashMap<>();

  8. Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods.

No comments:

Post a Comment