15 Java 8
Click here to load reader
-
Upload
federico-russo -
Category
Technology
-
view
130 -
download
0
Transcript of 15 Java 8
WELCOME TO JAVA 8Talk 15
JAVA 8
• Released in March 2014.
• No binary compatibility with Java 7.
• We are now at 1.8.0_u65.
• Support for Java 7 ended in April 2015.
• BBox 1.1 will be compiled on Java 8.
NEW LANGUAGE FEATURES
LAMBDA EXPRESSIONSbtn.onClick(new ClickListener() { @Override public void onClick(ClickEvent event) { System.out.println("Hello World!"); } });
btn.onClick( event -> System.out.println("Hello World!") );
Arrays.asList("a", "b", "d").forEach(e -> System.out.println(e));
METHOD REFERENCES
public class Utils { public static int compareByLength(String in, String out) { return in.length() - out.length(); } }
String[] args = new String[] {“microsoft","apple","linux","oracle"}; Arrays.sort(args, Utils::compareByLength);
DEFAULT METHODSprivate interface Defaultable { default String notRequired() { return "Default implementation"; } } private class DefaultableImpl implements Defaultable {} private class OverridableImpl implements Defaultable { @Override public String notRequired() { return "Overridden implementation"; }}
INTERFACE STATIC METHODS
private interface DefaultableFactory { static Defaultable create(Supplier<Defaulable> supplier) { return supplier.get(); }}
ANNOTATIONS
final Holder<String> holder = new @NonEmpty Holder<String>();
@NonEmpty Collection<@NonEmpty String> strings = new ArrayList<>();
OPTIONAL
Optional<String> fullName = Optional(null);
System.out.println("Full Name is set? " + fullName.isPresent());
System.out.println("Full Name: " + fullName.orElseGet(() -> “[none]”)); System.out.println(fullName.map(s -> "Hey " + s + “!") .orElse("Hey Stranger!"));
STREAMS
Collection<Task> tasks = Lists.newLinkedList();
double totalPoints = tasks .stream() .parallel() .filter(task -> task.getStatus() == Status.OPEN) .map(task -> task.getPoints()) // or map(Task::getPoints) .reduce(0, Integer::sum); System.out.println("Total points (all tasks): “ + totalPoints);
OTHER IMPROVEMENTS• Repeatable annotations
• Better type inference
• Runtime access to parameters names
• New date/time API
• Nashorn Javascript engine
• Parallel arrays
DELAYED TO JAVA 9
• Modularization
• Money and Currency API
• Java REPL shell