Pages

Tuesday, December 27, 2011

Java 7 Language Enhancements

This topic is part of OCP Java 7 Upgrade Exam - 1Z0-805 (Beta exam was 1Z1-805) Objective 1.


Language Enhancements [JSR 334]

Language enhancements were made as part of Project Coin.

1. String in the switch statement

Java 7 allows String object in switch statement and consequently String literals or final String constants in case. String.equals is used to match the case, hence matching is case sensitive.
[Ref]

2. Binary literals, numeric literals with underscores

Integral types - byte, short, int, and long - can be represented as binary numbers in Java 7. 
Examples: 0b010, 0B101
This representation is very similar to the hexadecimal representation using 0x. 
Using binary representation may be useful when dealing with bitmaps.
[Ref


It is sometimes difficult to read large numeric literals, requiring counting number of digits or zeros. With Java 7 enhancement, numbers like ten million and fifty thousand (10050000) can be represented as 10_050_000 to make it easier to read for humans.
[Ref]


3. try-with-resources

Instead of developer dealing with exceptions and closing appropriate resources in catch or finally block, Java 7 allows placing such resources within try parenthesis and automatically deals with closing such resources.
[Ref]
Sample:
try (
  Statement stmt = conn.createStatement();
  BufferedWriter bufw = new BufferedWriter(new FileWriter("foo.txt"));
) {
  ...
} catch(Exception e) {
  ...
}

4. Multi-catch in exception statements, 6. More precise rethrow in exceptions

Instead of catching each exception separately and handle them individually, Java 7 allows to catch and handle group of exceptions separated by "|" . Syntax is of the form:
catch (Exception1 | Exception2) {} 

Exam Watch:
  • If a catch block handles more than one exception type, then the catch parameter is implicitly final 
  • 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.



On rethrow, Java 7 is capable of determining the appropriate exception thrown based on the exceptions that a method throws. Example:


void method1() throws Exception1, Exception2 {
    try {
        ...
    } catch(Exception e) {
        throw e; // Java 7 can determine if Exception1 of 2 was throws here
    }
}


Exam Watch:
  • This analysis is disabled if the catch parameter is assigned to another value in the catch block.
  • If the catch parameter is assigned to another value, you must specify the exception type Exception in the throws clause of the method declaration.
  • When you declare one or more exception types in a catch clause, and rethrow the exception handled by this catch block, the compiler verifies that the type of the rethrown exception meets the following conditions:
    1. The try block is able to throw it.
    2. There are no other preceding catch blocks that can handle it.
    3. It is a subtype or supertype of one of the catch clause's exception parameters.
  • You can rethrow an exception that is a supertype of any of the types declared in the throws
[Ref]


5. The diamond operator with generic declarations

Java 7 allows using <> operator on the right side of a new construct, thereby simplifying generic declaration. Example:

List<String> names = new ArrayList<String>();
List<String> names = new ArrayList();  // Compiler generates warning
List<String> names = new ArrayList<>();  // <> should be used for auto type inferencing
<> operator may work when used within a method call, but recommended use is only in declaration.
[Ref



Other References:
- Talk/slides from the owner of Project Coin (JSR 334), Joe Darcy



No comments:

Post a Comment