Project Coin – Java 7 new features

After reading the title “project Coin“, few of you must be thinking that what is it ? And how it is related to Java 7 new features.

So, here we go…

Project Coin is the part of Java 7 development which is running since January 2009 with the aim of coming with small changes to the Java language.

In February to march 2009, there were nearly 70 proposals were submitted for huge range of possible changes in Java 7. Project coin has set up the example that how language can be developed in the future by discussion in openly manner with the public all around the world.

Java 7 new Features :

Support of Strings in “Switch” statement:
Before Java 7, we can only use constants of type byte, char, short or enum. However, now we can use the string also in switch case statement.

switch(dayOfWeek)
{
   case "Monday" : System.out.println("Its first day of the office");break;
   ....
   case "Sunday" : System.out.println("Hurrey.. its weekend");break;
}

Enhanced Syntax of Numeric literals

  • Numeric constants expressed as binary.
  • Use of underscores in integer constants for readability.
  • A specific suffix to denote that an integer constant has type short or byte.

Improved Exception handling
Before Java 7, we used to write multiple catch blocks in our program which looks like very verbose.
with the help of pipe operator in Java 7, we can write multiple catch blocks in one line as shown below :

public void printFileContent(String fileName_) {
Configuration cfg = null;
try {
String fileText = getFile(fileName_);
//...code to print content
} catch (FileNotFoundException|ParseException|FileLockInterruptionException e) {
System.err.println("error while opening file");
} catch (IOException iox) {
System.err.println("Error while processing file");
}

}

Try with resources

try ( FileOutputStream fos = new FileOutputStream(file);
InputStream is = url.openStream() ) {
byte[] buf = new byte[4096];
int len;
while ((len = is.read(buf)) > 0) {
fos.write(buf, 0, len);
}
}

Diamond Syntax :
Before Java 7, generic Syntax was like :

Map<Movie,Songs> m = new HashMap<Movie,Songs>();

As you can see, we are repeating the syntax at right hand side, why compiler should not be able to take care of that?
Below is the new user friendly syntax.

Map<Movie,Songs> m = new HashMap<>();

There is also change in Varargs Syntax which we will discuss in detail later on.
Please leave your valuable suggestions to improve the article and content.

Advertisements

About Sanju
I am Software Programmer. I am working in JAVA/J2EE Technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: