![]() |
Java Development Kit 7 |
Neste link você encontra um sumário das novidades desta release.
Improved Exception Handling – Multi-Catch ( JSR 334 )
try {
final Class clazz = Class.forName(className);
final Class[] ctorParams = {String.class, String.class, Integer.TYPE};
final Constructor ctor = clazz.getConstructor(ctorParams);
final Person person = (Person) ctor.newInstance(newLastName, newFirstName, newAge);
final Method toStringMethod = clazz.getDeclaredMethod(methodName);
out.println(toStringMethod.invoke(person));
}catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException| InvocationTargetException | InstantiationException reflectionEx){
err.println( “Reflection error trying to invoke ” + className + “.” + methodName + “: ” + reflectionEx);
}
Strings in Switch
public static void main(final String[] arguments){
final String name = “Dustin”;
switch (name) {
case “Dino” :
out.println(“Flintstones?”);
break;
case “Neo” :
out.println(“Matrix?”);
break;
case “Gandalf” :
out.println(“Lord of the Rings?”);
break;
case “Dustin” :
out.println(“Inspired by Actual Events”);
break;
default :
out.println(“The Good, the Bad, and the Ugly?”);
}
}
Binary integral literals and underscores in numeric literals
int
maxInteger = 0b01111111111111111111111111111111;
int
maxInteger = 0b0111_1111_1111_1111_1111_1111_1111_1111;
Try With-Resources Statement
Muitos que trabalham com Java a mais tempo ja devem ter notado o quanto é difícil e chato trabalhar com recursos que necessitam de fechamento explicito. ( Conexao de Banco, Arquivos e etc )
Um exemplo disso é o código logo abaixo, percebam o quando de código extra foi necessario, apenas para fechamento de recursos.
private static void customBufferStreamCopy(File source, File target) {
InputStream fis = null;
OutputStream fos = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(target);
byte[] buf = new byte[8192];
int i;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
}catch (Exception e) {
e.printStackTrace();
} finally {
close(fis);
close(fos);
}
}
private static void close(Closeable closable) {
if (closable != null) {
try {
closable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Agora nesta nova versão, todos os recursos são automaticamente fechados, após a execução do bloco Try, sendo assim, o código anterior pode ser substituido por este:
try (
InputStream fis = new FileInputStream(source);
OutputStream fos = new FileOutputStream(target)){
byte[] buf = new byte[8192];
int i;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
}catch (Exception e) {
e.printStackTrace();
}
}
Simplified Varargs Method Invocation
Antes da mudança
static List asList(T… elements) { … }
static List<callable> stringFactories() {
Callable a, b, c;
…
*// Warning: **”uses unchecked or unsafe operations”*
return asList(a, b, c);
}</callable
Depois da mudança
*// Warning: **”enables unsafe generic array creation”*
static List asList(T… elements) { … }
static List<callable> stringFactories() {</callable
Callable a, b, c;
…
return asList(a, b, c);
}
Performance na JDK 7
Alguns artigos que recomendo:
http://lingpipe-blog.com/2009/03/30/jdk-7-twice-as-fast-as-jdk-6-for-arrays-and-arithmetic/
http://download.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html
Abrcs
Natanael Fonseca