Saturday, April 5, 2014

What is new in Java 7? New useful features

Java 7 is everywhere now and your boss maybe thinking to migrate too. So if you are still coding old way, it's useful to sneak peek the features.

Java has been constantly growing so much bigger that I cannot fathom all that is added to it.
Let me sum up the ones I found useful in my daily use. Consider this an introduction to Java 7 and we’ll be rolling out more detailed posts on each topic eventually.

String in switch case:

Let me start with a favourite,
With java 7, we can use string in switch case!
switch(str){ //str of type String
  case "abc": //do something
  case "efg": //do other thing

Multiple Exception Handling:
No need to copy paste many catch expressions. We can bundle related exceptions in a single catch.
get_A_Coffee (); //do someting;
catch(NotTastyException, NotHotEnoughException coffeExpObj){ //bundle similar exceptions
throwCoffeAndGetNew(coffeExpObj); //handle

Type Inference:
Lists can be put as such easily!
 Map<String, Integer> = new HashMap<String, Integer>();  //no need to write like this
 Map<String, Integer> = new HashMap<>();     //this will just do

Elvis Operator:
This is from groovy language. ( ?: looks like Elvis)
Instead of  x!=null? x: y, we can write as x ?: y
Ex:- Integer ival = (input from user); // may be null
int i = ival ?: -1; // no NullPointerException from unboxing

We hear something called Module in Java 7, module or a module definition by book is ‘a logical unit of set of files, resources and its dependencies that can be versioned, packaged and deployed in the module repository for re-use by some other application.’ Each module consists of a module meta-data that is self-describing. (Good for JAR versioning). Modules can be exported & imported between projects.

/* module meta-data */
module A @ 1.0 //this module class is of version 1.0
requires B @ 2.1; //this needs B of version 2.1
requires C @ 1.1; //this needs C of version 1.1
} /*meta-data end*/

module A;
package com.sample;
public class sample class{ 
public void sample()

Note: If class or one of its members or constructors is declared module, it will be accessible from a type that belongs to the same module.
If you don’t get it chill, I didn’t either, at first. We’ll see more on this in following posts.
Whole new I/O API:
The Java team has rolled out a whole new IO package, java.nio(new IO)
Tons of useful things for file and path handling under this.
Path is one such new class. (
It’s like a Unix path for manipulation, finding file, properties etc
Path searchPath = Paths.get(“c:/sample”);
final Path findFile = Paths.get(“samplefile”);
//similar to FileSystems.getDefault().getPath("/samplefile");
FileVisitor visitor = new SimpleFileVisitor()
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
//do something
Files.walkFileTree(searcPath, visitor);
Other features of nio are Buffers, Channels, Selectors, Character sets, asynchronous I/O, File Notifications, Directory Operations. know more

Garbage First!:
No more chaos with a fuzzy GC,
•    All new garbage collector, the “Garbage First Collector”.
•    Memory split into multiple regions as opposed to 2 regions in the last version.
•    Quite predictable and provides greater through-put for memory intensive applications.
•    Performs faster than the last parallel collectors.

There are more features like Fork and Join, Cache API (temporary, permanent), new Date and time API. Stay tuned to pro them too.

1 comment:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai