Contenu connexe
Similaire à Novidades do Java SE 8 (20)
Plus de Bruno Borges (20)
Novidades do Java SE 8
- 1. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 132
Lambda, Nashorn,
Metaspace: algumas
novidades do Java SE 8
Bruno Borges
Oracle Product Manager
Java Evangelist
@brunoborges
- 2. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 133
Bruno Borges
Oracle Product Manager / Evangelist
Desenvolvedor, Gamer, Beer Sommelier
Entusiasta em Java Embedded e JavaFX
Twitter: @brunoborges
- 3. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 134Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16
Agenda
História
Scene Graph
Java API
Properties
Bindings
Controls
CSS
WebView
JFXPanel
Charts
- 4. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 135
SE8
- 5. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 136
Um pouco da história
Green Team, C++ ++ –, Oak - 1990
Java 1.0 / 1.1 – 1996 / 1997
Java 2 “J2SE” 1.2 – 1998
Java 1.3 ”J2SE 1.3” – 2000
Java 1.4 ”J2SE 1.4” – 2002
Java 1.5 “Java SE 5” - 2004
Java 1.6 “Java SE 6” - 2006
Java 1.7 “Java SE 7” - 2011
- 6. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 137
E o futuro...
- 7. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 138
E o JavaFX?
É o sucessor do
Java Swing
- 8. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 139
Disponível para...
Windows, Linux, Mac OS X
E em Preview...
ARM*
Apple iOS*
Android*
JavaFX 2.2 vem junto com JDK 7u6+
Standalone para Java 6
- 9. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1310
OpenJFX
JavaFX open sourced!
http://openjdk.java.net/projects/openjfx/
- 10. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1311Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16
Java
SE 8
Melhorias em interfaces
- 11. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1312
Java SE 8 – Melhorias em Interfaces
Static methods
Métodos com implementação default
Functional Interfaces
toda interface que define apenas 1 método abstrato (sem corpo)
@FunctionalInterface: similar a @Override, para garantia
public default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
- 12. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1313Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16
Java
SE 8
Melhorias em interfaces
Lambdas
- 13. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1314
Java SE 8 – Lambdas
Lista de inputs tipados à esquerda, bloco com retorno à direita
Input à esquerda, void à direita
Métodos estáticos e de objetos como funções lambda
(int x, int y) -> { return x + y; }
(x, y) -> x + y
x -> x * x
() -> x
x -> { System.out.println(x); }
String::valueOf x -> String.valueOf(x)
Object::toString x -> x.toString()
x::toString () -> x.toString()
ArrayList::new () -> new ArrayList<>()
- 14. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1315
Java SE 8 – Lambdas (cont.)
Em busca de um match para saber qual construtor/método chamar
O método compare precisa de dois parâmetros, e deve retornar
int. A expressão lambda condiz com esta assinatura, logo a
expressão é válida
Expressões não devem modificar variáveis definidas fora do corpo
lambda
Comparator<String> c = (a, b) -> Integer.compare(a.length(),
b.length());
int count = 0;
List<String> strings = Arrays.asList("a", "b", "c");
strings.forEach(s -> {
count++; // error: can't modify the value of count
});
- 15. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1316
Java SE 8 – Lambdas (cont.)
Classes abstratas não podem ser instanciadas com lambda
poderia esconder código (construtor por exemplo)
elimina possibilidade de otimizações futuras
Solução: factory methods
Ordering<String> order = (a, b) -> ...;
CacheLoader<String, String> loader = (key) -> ...;
Ordering<String> order = Ordering.from((a, b) -> ...);
CacheLoader<String, String> loader =
CacheLoader.from((key) -> ...);
- 16. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1317
Java SE 8 – Lambdas (cont.)
Novos pacotes:
java.util.stream: suporta operações em valores de stream, com
expressões lambda
java.util.function: interfaces funcionais utilitárias do JDK
int sumOfWeights = blocks.stream().filter(b -> b.getColor() == RED)
.mapToInt(b -> b.getWeight())
.sum();
// In Java 7:
foo(Utility.<Type>bar());
Utility.<Type>foo().bar();
// In Java 8:
foo(Utility.bar());
Utility.foo().bar();
- 17. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1318
Java SE 8 – Lambdas: Antes e Depois
public void emailDraftees(List<Person> pl) {
for(Person p : pl){
if (p.getAge() >= 18 &&
p.getAge() <= 25 &&
p.getGender() == Gender.MALE) {
roboEmail(p);
}
}
}
- 18. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1319
Java SE 8 – Lambdas: Antes e Depois
public void emailDraftees(List<Person> pl) {
for(Person p : pl) {
if (isDraftee(p)) {
roboEmail(p);
}
}
}
public boolean isDraftee(Person p){
return p.getAge() >= 18
&& p.getAge() <= 25
&& p.getGender() == Gender.MALE;
}
- 19. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1320
Java SE 8 – Lambdas: Antes e Depois
Predicate<Person> draftees;
draftees = p -> p.getAge() >= 18 &&
p.getAge() <= 25 &&
p.getGender() == Gender.MALE;
robo.emailContacts(pl, allDraftees);
public void emailContacts(List<Person> pl, Predicate<Person> pred) {
for(Person p : pl)
if (pred.test(p))
roboEmail(p);
}
- 20. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1321Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16
Java
SE 8
Melhorias em interfaces
Lambdas
Generics
- 21. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1322
Java SE 8 – Generics
Inferência de tipos genéricos
// In Java 7:
foo(Utility.<Type>bar());
Utility.<Type>foo().bar();
// In Java 8:
foo(Utility.bar());
Utility.foo().bar();
- 22. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1323Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16
Java
SE 8
Melhorias em interfaces
Lambdas
Generics
Date and Time API
- 23. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1324
Java SE 8 – Date and Time
Mudança total da API para lidar com data, hora, calendário
Baseado no JodaTime – JSR 310
Novas classes:
LocalDateTime, LocalDate, LocalTime
Year, YearMonth, Month, MonthDay, DayOfWeek
Instant, ZonedDateTime, OffsetTime, Duration, Period
- 24. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1325Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16
Java
SE 8
Melhorias em interfaces
Lambdas
Generics
Date and Time API
Outras APIs
- 25. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1326
Java SE 8 – Outras mudanças de API
Reflection API
Manipular lambdas, anotações, etc
Annotations
Permite definir anotação no tipo genérico
Novos métodos em IO/NIO (busca recursiva de arquivo/diretorio)
Concurrency API
Collections API
List<@Nullable String>
- 26. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1327
DEMO
Antes e depois do Java SE 8
- 27. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1328
Nashorn
Javascript
the right way
- 28. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1329
Oracle Nashorn?
É o sucessor do
Mozilla Rhino
- 29. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1330
Nashorn
Engine de processamento da linguagem Javascript
Escrito do zero
Seguindo boas práticas
Novas técnicas e algoritmos
Atento às otimizações da JVM (ex: invokedynamic)
Projeto mantido pela Oracle, e Open Source
Incluído no OpenJDK em 21/12/12
- 30. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1331
Nashorn – Diferenciais?
Maior integração com a camada Java
Integração com aplicações JavaFX
Utilizado nos componentes WebView e HTML5
Maior performance
Menor footprint de memória
- 31. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1332
DEMO
JavaFX usando Nashorn
- 32. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1333
Metaspace
Say goodbye to
OutOfMemoryError
- 33. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1334
Metaspace
Substitui a PermGen
Modelo já utilizado pela JVM Oracle JRockit
Por padrão, o tamanho é variável – ótimo para desenvolvimento
Em produção, deve ser limitado
– novo parâmetro: -XX:MaxMetaspaceSize
– parâmetros *PermGen ignorados pela VM
Dados armazenados “off-heap”
Limitado ao tamanho de memória disponível na máquina
- 34. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1335
Perguntas?
- 35. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1336
OBRIGADO!
@brunoborges
blogs.oracle.com/brunoborges
- 36. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1337
The preceding is intended to outline our general product direction. It is intended
for information purposes only, and may not be incorporated into any contract.
It is not a commitment to deliver any material, code, or functionality, and should
not be relied upon in making purchasing decisions. The development, release,
and timing of any features or functionality described for Oracle’s products
remains at the sole discretion of Oracle.
- 37. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1338