Skip to main content

Welcome java 7 - Part 2 JSR 334 Coin

Posted by otaviojava on August 21, 2011 at 1:29 PM PDT

Given some information about Java 7, i will now show the imrovements to the latest version.In this article i use the netbeans version 7.0.1, for this you need to download the JDK 7 (the link is below the article). After downloading and installing Netbeans (but during the installation remember to select the JDK 7 path).

 
The first example will show switch with String, previously this functionality was only possible  with Enuns 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 cacth 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 <T> List<T> asList (T... elements) {
System.out.println(elements);
return null;
}
@SafeVarargs
static void varags(List<String>... stringLists) {
Object[] array = stringLists;
List<Integer> 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 caracter “_” 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

 

 

 

Comments

Thanx for the information, simple and clear. But can you ...

Thanx for the information, simple and clear. But can you please explain more about @SafeVarargs

Applying this annotation to a method or constructor ...

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.

&nbsp;Java 7 is supported by IntelliJ for quite some time. ...

Java 7 is supported by IntelliJ for quite some time. The current Eclipse 3.8 supports it too and quite nicely.

thank you for this information.

thank you for this information.