Project Coin – Java 7 new features

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 1 Comment on Project Coin – Java 7 new features

What is Project Coin and how it is related to JAVA

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
C# developers will find the syntax friendly , as it is same as using clause.

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.

 

Related posts

One thought on “Project Coin – Java 7 new features”

Leave a Reply

Your email address will not be published. Required fields are marked *