Welcome to Java7 Quick tour

Hi,

While browsing some information about Java 7, I came across a very nice blog covering few major changes in Java 7. Would like to share with all my blog users. Here it goes..

The first example will show switch with String, previously this functionality was only possible  with Enums and integer values. In actual fact the JDK retrieves the hashcode for the String which is an integer. Below is an example of this feature.

String drink=”coffee”;
switch (drink){
case “coffee”:
System.out.println(“So you need milk”);
break;
case “juice”:
System.out.println(“So you need sugar”);
break;
case “refrigerate”:
System.out.println(“So you need ice”);
break;
default:
System.out.println(“unknown drink “);
break;
}
I will now show you the ARM, Automatic Resource management, you don’t need to concern yourself with the resources that will be used in your program because it will automatically close when it exits the Try block. For this just implement the interface java.lang.AutoCloseable, the only method is the Close, The AutoCloseable is the better option than Closeable because an exception is not thrown when you close the resource, in the second picture we can see this.
public void copyFile(File original, File copy) throws FileNotFoundException, IOException {
try (
InputStream in = new FileInputStream(original);
OutputStream out = new FileOutputStream(copy)) {
byte[] buf = new byte[1024];
int n;
while ((n = in.read(buf)) >= 0) {
out.write(buf, 0, n);
}
}// it is automatically close
}
The multi-try, for some people is the most important feature in this version, it now allows many exceptions inside the catch block just separate with a “|” pipe.
ExemploARM arm=new ExemploARM();
try {
arm.copyFile(origem, destino);
catch (FileNotFoundException | IOException ex) {
ex.printStackTrace();
System.out.println(“It’s can’t copy file”);
}
using multy-try
In Java 7 there are some improvements to Generics and collections making it easy to make this type Object. Now it is possible to make generic collections easily with the  diamond operator “<>”
List<Object> diamond=new ArrayList<>(); // diamond
List<Drink> Drinks;
Map<String, List<Drink>> maps=new HashMap<>();
maps.put(“diamond”, drinks=new ArrayList<>() );
maps.put(“other example”, new ArrayList<Bebida>() );
maps.put(“erro”, new ArrayList<>() );
[/code] Picture 4: diamond
Talking more about generic collections there is the annotation @SafeVarargs for ensuring this method is safe.
 Applying this annotation to a method or constructor suppresses unchecked warnings about a non-reifiable variable-arity (vararg) type and suppresses unchecked warnings about parameterized array creation at call sites.
@SafeVarargs
static List asList (T… elements) {
System.out.println(elements);
return null;
}
@SafeVarargs
static void varags(List… stringLists) {
Object[] array = stringLists;
List tmpList = Arrays.asList(42);
array[0] = tmpList; //run with warning
String s = stringLists[0].get(0); // ClassCastException
}
@SafeVarargs
The digit separator allows for good understand when writing big numbers in java code, the only rule is you can’t separate the last and the first number, now you can write separator numbers with the character “_” it is also possible to write Double values and Float values, for example, for the JDK is equals 22 and 2_2. There is also literal in binary, which is most important when programming in embedding devices,  just put “ob” (zero and b) in front of a number, this Features can also use the separator.
long longPrimitive=9_999_999_99;
Long longObjete=9__3234_300l;
double doublePrimitive=232_32.32_12d;
Double doubleObjeto=88_32.32_12d;
int binA=0b01_01;
int binB=0b0101_0111;
if(2222==22_22){
System.out.println(“equals values”);
}
if(binA==5){
System.out.println(“equals binary values”);
}
picture 5: using separator and literal binary.
Other feature interesting is try with resource now it possible instantiate one variable if it does not generate an exception.
BufferedWriter writer=null;
try {
writer = Files.newBufferedWriter(arquivo, charset);
writer.write(s, 0, s.length());
catch (IOException x) {
System.err.format(“IOException: %s%n”, x);
}
Picture 6: before was necessary create the variable
try (BufferedWriter writer = Files.newBufferedWriter(file, charset)) {
writer.write(s, 0, s.length());
catch (IOException x) {
System.err.format(“IOException: %s%n”, x);
}
Picture 7: after using try with resource in java 7 

Some more features..

 

[Source]
http://weblogs.java.net/blog/otaviojava/archive/2011/08/21/welcome-java-7-part-2-jsr-334-coin-0
http://marxsoftware.blogspot.com/2011/03/jdk-7-new-interfaces-classes-enums-and.html


Thanks
R Vashi

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