Donation

If you found the contents in this blog useful, then please make a donation to keep this blog running. You can make donations via Skrill with email address shazin.sadakath@gmail.com

Tuesday, August 9, 2011

Java 7 Hands On

I have been trying out Java 7 this week and Tested out the latest features in JDK 7. They have given some serious thought on minimizing the coding strain of the developer by providing some new features. My favorite one is Binary Literals. There are quite more features which are;

  • Try with Resources
  • Multi Catch Statements
  • Diamond Syntax
  • Binary Literals
  • Underscore Between Literals
  • String in Switch
I wrote a small class to Test out all those features which is shown below.
import java.io.*;

import java.awt.Color;
import java.util.*;

public class Test {
public static void main(String[] args)throws IOException {
try{
try (FileInputStream in = new FileInputStream(args[0])) { // Try with Resources


int i = in.read();

}
} catch(FileNotFoundException | ArrayIndexOutOfBoundsException e) { // Multi Catch Statements
e.printStackTrace();
}


List<Color> colors = new ArrayList<>(); // Diamond Syntax

int no = 4 << 1;
int bi8 = 0b1000; // Binary Literals

System.out.println(" 4 << 1 == 0b1000 > "+(no == bi8));

int oldOneMillion = 1000000;
int newOneMillion = 1_000_000; // Underscore Between Literals

System.out.println("1000000 == 1_000_000 ? "+(oldOneMillion == newOneMillion));


/* String in Switch */

final String RED = "Red";
final String GREEN = "Green";
final String BLUE = "Blue";

Scanner sn = new Scanner(System.in);
System.out.print("Enter choice (Red, Green, Blue): ");
String input = sn.nextLine();
switch(input) {
case RED :
colors.add(Color.RED);
break;
case GREEN :
colors.add(Color.GREEN);
break;
case BLUE :
colors.add(Color.BLUE);
break;
default :
System.out.println("None");
}
}
}


Great Features, Thanks Sun/Oracle Engineers. Hope you all fix the minor bugs soon!

No comments: