Saturday, July 30, 2011

What can interest you in Java 7

            Java 7 is out now. Before I become proficient in Mustang, Dolphin is out. Yeah Dolphin is a codename of Java 7. But within that we have seen many stories to not to use Java 7. We’ll come to that later. Now we’ll see what’s special in Dolphin version. These are the attracting features of Java 7,


Strings in Switch:

            Not only Integers, we can use String objects also with Switch loop

Binary and underscores in numeral literals:

            We can use the binaries directly and also for long numeric literals we can separate by underscore.

int one_million = 1_000_000;
int eight = 0b1000;

Diamond Syntax:

            One of the cool feature, It reduces Java's verbosity surrounding generics by having the compiler infer parameter types for constructors of generic classes. Diamond operator <> infers the type from the reference declaration.

List<String> list = new ArrayList<>();

Multi-Catch Exceptions:

            It’s another cool stuff. We can catch more than one exception in a single catch block by separating the Exceptions with pipe symbol.

try {
    // block of statments
} catch(Exception1|Exception2|Exception3...) {
    // block of statements.
}

Try with Resources:

            Normally we should be very cautious on resources. It will situate into memory leak if we don’t terminate the resource correctly. Java 7 natively supports our resources.  Resources are scoped within try block and closed automatically without our finally stuff

try (InputStream in = new FileInputStream("file.txt")) {
    System.out.println(in.read());     
}

Safe Varargs

            When we tried to invoke a *varargs* (variable arity) method with a non-reifiable varargs type, the compiler currently generates an “unsafe operation” warning. JDK 7 moves the warning from the call site to the method declaration. A new annotation @SafeVarargs will suppress this warning at both declaration site and the call site.

             Yes I missed some which I’m not very clear. This can definitely help me if suppose I’m sacked for the upcoming recession 

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...