Java 7 features

All

Intro

Preparing myself here for some interviews and reading about Java, I came to review some features that were added to Java 7 and Java 8. First, let start with J7:

Java 7 features

Diamond operator

The diamond operator is a way to facilitate the declaration of variables, saving time and makes the code less confusing.

Map<String, List<Trade>> trades = new TreeMap<String, List<Trade>> ();
Map<String, List<Trade>> trades = new TreeMap<> ();

 

String in the switch statement:

switch(status) {
    caseX:  x();
    caseY:  y();
    default:
            do_something()
}

try optimization

No need to use the finally mandatorily as before we can do only:

   try(){

} catch(Exception){

}

Exception handling at the same time

   try(){

} catch(Exception1 | Exception2 | Exception3){

}

Path

It’s so useful just use path, get the file and use it.

WatchService

I didn’t know about this feature, which is quite interesting btw. You can basically look at the file changes modification and be notified by it. Cool. But you actually need to create a service to watch the file.

WatchKey key = watchService.take();

invokedynamic Feature

Called Java secret weapon [2]. Interestingly to learn about this I watched [3], describing the use of invokevirtual – which you can see it on bytecode.

This is a feature that is explored much more in J8[4]

It enables the use of dynamic languages above the JVM. I will create a separated article about it later.

REFs

[1] https://www.oreilly.com/learning/java7-features

[2]https://www.infoq.com/articles/Invokedynamic-Javas-secret-weapon

[3] https://www.youtube.com/watch?v=262lTMIpYQE

[4] https://www.youtube.com/watch?v=0vfFHMGESVQ