SlideShare a Scribd company logo
Java Reborn
Martijn Blankestijn
&
Remko de Jong
16:00 – 17:30
Presentation
17:30 – 18:15
Food
18:15 – 20:30
Lab
20:30 – 22:00
Drinks
Presentation
• JSR 310 – DateTime API
• Enhanced Metadata (inc. JSR 308)
• Miscellaneous changes
• JSR 335 – Project Lambda
• Completable Futures
NLJUG University Sessie: Java Reborn, Powered by Ordina
new Date(2013, 12, 31);
Sat Jan 31 00:00:00 CET 3914
1900 + 2013
0-based
Who uses
Date?
Calendar?
Joda-time?
Why not Joda-Time?
• No separation between
human and machine timelines
• Pluggable chronology
(e.g. Koptic calendar)
• Nulls
Goal of new Date-Time API
Immutable
Type-safe
@Deprecated
java.util.Date
java.util.Calendar
java.util.Timezone
java.text.DateFormat
http://www.threeten.org
Human vs Machine (time)
Human Time
Human notions
• Tomorrow
• Thursday, March
20th 2014
• Two hours ago
• Last year
DateTime API
• LocalDate
• LocalTime
• LocalDateTime
• Year
• YearMonth
• Day
Machine Time
past future
Java Time Scale (in nanos)
java.time.Instant
Date arithmetic
date.plusDays(40);
date.plus(40, ChronoUnit.DAYS);
date.withDayOfMonth(5);
date.with(TemporalAdjusters.lastDayOfMonth());
date.with(lastDayOfMonth());
date.with(nextOrSame(FRIDAY));
build your own!
Composing
Year year = Year.of(2014);
YearMonth yMonth = year.atMonth(MARCH);
LocalDate date = yMonth.atDay(25);
Year.of(2014)
.atMonth(MARCH)
.atDay(25);
== LocalDate.of(2014, MARCH, 25);
New objects
Amounts - Duration
Time based (Java Time Scale)
Storage in nanoseconds
Duration d = Duration.ofHours(6);
// After 6 hours
LocalDateTime.now().plus(d);
Amounts - Period
Date based
Years, months and days (ISO Calendar)
Period p = Period.ofDays(10)
.plusMonths(1);
LocalDate.now().plus(p);
How many seconds in a day?
a) 86.401
b) 86.400
c) 90.000
d) 82.800
e) 86.399
ZoneId z = ZoneId.of("Europe/Amsterdam");
Duration duration = Duration.ofDays(1);
ZonedDateTime mrt30 =
ZonedDateTime.of(
LocalDateTime.of(2014,3,30,0,0,0), z);
mrt30.plus(duration);
2014-03-31T01:00+02:00[Europe/Amsterdam]
DateTime API Duration
Period period = Period.ofDays(1);
ZonedDateTime mrt31 = mrt30.plus(period);
mrt31.toInstant().getEpochSecond()
- mrt30.toInstant().getEpochSecond();
82.800 seconds !!!!
DateTime API Period
March, 30 2014
00:00
March, 31 2014
00:00
02:00 03:00
Period of 1 day (23 * 60 * 60 seconds)
Duration of 1 day (24 * 60 * 60 seconds)
01:00
Complexity of Time
DateTimeFormatter
DateTimeFormatter
.ofPattern("dd-MM-yyyy HH:mm:ss");
Thread-safe!!!
NLJUG University Sessie: Java Reborn, Powered by Ordina
Parameter Names
Repeating @nnotations
Type @nnotations
Enhanced Metadata
@Path("persons/{id}")
public Person get(@PathParam("id") int id)
{
@PathParam("id")
Parameter Names
void print(java.lang.reflect.Method m) {
for (Parameter p : m.getParameters())
if (p.isNamePresent()) {
System.out.println(p.getName());
}
}
}
javac -target 1.8 -source 1.8 -parameters
Parameter Names: Opt-in
void invoke(Method method,
Map<String,Object> params) {
Object[] args =
Stream.of(method.getParameters())
.map(p->params.get(p.getName()))
.toArray();
method.invoke(resource, args);
}
Parameter Names: Example
@AttributeOverride(name = "streetno" ...)
private Address residentialAddress;
@AttributeOverrides({
@AttributeOverride(name = "streetno" ...,
@AttributeOverride(name = "houseno" …)
})
private Address residentialAddress;
Repeatable Annotations
Container - pattern
JSR 308 extends
Java’s annotation system
so that
annotations may appear
on any use of a type
Type annotations make
Java's annotation system
more expressive and uniform.
Type Annotations Goal
Syntax (JSR 308)
+
Annotation processing capability (JSR 269)
=
Pluggable type-checking
Libraries:
●http://types.cs.washington.edu/checker-framework/
●...
Type Annotations - Enablers
@NonNull List<@Interned String> messages;
@Interned String @NonNull[] array;
LocalDate d = (@ReadOnly LocalDate) nu;
String toString(@ReadOnly ThisClass this) {}
public @Interned String intern() {}
New Annotation Locations
Type checking prevents mistakes…
… but not enough
Null Pointer Exceptions
Wrong String Comparisons
Fake Enums
Units (meters/yards, kilogram/pounds)
Why Type Checkers?
/**
* @param distance in kilometers
* @return will I make it?
*/
boolean hasEnoughFuel(double distance) {
return
distance < velocity * maxFlightTime;
}
Will I make it?
double distance = 1200; // km
plane.hasEnoughFuel(distance);
NLJUG University Sessie: Java Reborn, Powered by Ordina
/**
* @param distance in kilometers
* @return will I make it?
*/
boolean hasEnoughFuel(@km double distance) {
return
distance < velocity * maxFlightTime;
}
How about now?
@km double distance = (@km double) 1200
plane.hasEnoughFuel(distance);
other changes
Set<String> x = new HashSet<>();
Set<String> s = new
HashSet<>(Collections.<String>emptySet());
Set<String> s =
new HashSet<>(Collections.emptySet());
JDK 8
Diamond
Operator
Improved Type Inference
public static void main(String[] args) {
print(Arrays.asList(1, 2, 3));
}
static void print(Object o) {
out.println("Object o");
}
static void print(List<Number> ln) {
out.println("List<Number> ln");
}
What does
it print ??
This
JDK 1.8
JDK 1.7
Corner cases
RMI-IIOP
CORBA
AWT / SWING / SOUND
Full: 38.4
3: 19.5 MB
Security
JAXP (Crypto)
JMX
Instrumentation
2: 17.5 MB
JDBC
RMI
XML JAXP
1= 13.8 MB
java.util *
Reflection
Collections
DateTime
Java Core
IO
Security
Script
Crypto
Compact Profiles
Miscellaneous changes
• Nashorn
• PermGen
• Base64.Encoder / Base64.Decoder
• APT
http://openjdk.java.net/projects/jdk8/features
Generated by: https://www.jasondavies.com/wordcloud
NLJUG University Sessie: Java Reborn, Powered by Ordina
+ Lambda expressions and method
references
+ Enhanced type inference and target
typing
+ Default and static methods in
interfaces
Contents of JSR 335 or Lambda
“To enable programming
patterns
that require modeling code as
data
to be convenient and idiomatic
in Java.”
Goal
idiomatic
Line breaks: idiom|at¦ic
Pronunciation: /ˌɪdɪəˈmatɪk /
ADJECTIVE
1 Using, containing, or denoting expressions that are
natural to a native speaker:
‘he spoke fluent, idiomatic English’
NLJUG University Sessie: Java Reborn, Powered by Ordina
Paradigm shift
from
how  what
or
imperative  declarative
{ live coding; }
Quick Recap
“The Metamorphosis”
Predicate<Integer> isEven =
new Predicate<Integer>() {
@Override
public boolean test(Integer i) {
return i % 2 == 0;
}
};
With inner class
Predicate<Integer> isEven =
(Integer i) -> { return i % 2 == 0; };
Full lambda notation
The type of a lambda is just a plain old interface
Predicate<Integer> isEven =
(i) -> { return i % 2 == 0; };
The type is inferred from the
context
Predicate<Integer> isEven =
(i) -> { i % 2 == 0; };
Implicit return for expressions
Predicate<Integer> isEven =
i -> i % 2 == 0;
Removing parentheses
Predicate<Integer> isEven =
Class::isEven;
static boolean isEven(Integer i) {
return i % 2==0;
}
Using a method reference
functional interfaces
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
<- backward compatible
public interface Runnable {
void run();
}
public interface ActionListener … {
void actionPerformed(ActionEvent e);
}
public interface PrivilegedAction<T> {
T run();
}
@FunctionalInterface
public class ThreadExample {
public static void main(String[] args) {
new Thread(
new Runnable() {
@Override
public void run() {
System.out.println(“Hi!");
}
}
).start();
}
}
@FunctionalInterface
public class ThreadExample {
public static void main(String[] args) {
new Thread(
() -> System.out.println(“Hi!")
).start();
}
}
@FunctionalInterface
new FunctionalInterface() {
@Override
public T someMethod(args) {
body
}
});
args ->
Replace
With body }{
General approach
@FunctionalInterface
@FunctionalInterface
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
@FunctionalInterface
• Catch errors @ compile time
• Communicate intention
• … but not required
Why?
@FunctionalInterface
Predicate<T> -- boolean test(T t)
Consumer<T> -- void accept(T t)
Function<T,R> -- R apply(T t)
Supplier<T> -- T apply()
package java.util.function;
@FunctionalInterface
So the type of a lambda
is a functional interface…
() -> "done"; // ?
Supplier<String> option1 = () -> "done";
Callable<String> option2 = () -> "done";
PrivilegedAction<String> option3 =
() -> "done";
() -> "done";
the type is inferred from the
context
The type is inferred from the context
Supplier<Runnable> c =
() -> () -> out.println("hi");
// Illegal, cannot determine interface
Object o =
() -> out.println("hi");
// Valid, explicit cast
Object o =
(Runnable) () -> out.println("hi");
System.out::println
Types of Method Reference
1.ContainingClass::staticMethodName
2.ContainingObject::instanceMethodName
3.ContainingType::instanceMethodName
4.ClassName::new
class Person {
String name;
LocalDate bday;
public int getName() { return name; }
public LocalDate getBirthday() {
return bday;
}
public static int compareByAge(
Person a, Person b) {
return a.bday.compareTo(b.bday);
}
}
Reference to a static method
Person::compareByAge
class Person {
…
public static int compareByAge(
Person a, Person b) {
return a.bday.compareTo(b.bday);
}
}
(a1, a2) -> Person.compareByAge(a1, a2)
Reference to an Instance Method of a
Particular Object
person::getBirthDay
class Person {
…
public LocalDate getBirthday() {
return birthday;
}
}
p -> p.getBirthDay()
Reference to an Instance Method of an
Arbitrary Object of a Particular Type
String::startsWith
public boolean startsWith(String prefix)
{
return startsWith(prefix, 0);
}
(s1, s2) -> s1.startsWith(s2)
BiFunction
Reference to a constructor
Person::new
class Person {
…
public Person() {
// Default constructor
}
}
() -> new Person()
{ scope }
import static java.lang.System.out;
public class Hello {
Runnable r1 = () -> out.println(this);
Runnable r2 = () -> out.println(toString());
public String toString() {
return "Hello, world!";
}
public static void main(String... args) {
new Hello().r1.run();
new Hello().r2.run();
}
}
public static void main(String[] args) {
int x = 5;
Function<Integer, Integer> f1 =
new Function<Integer, Integer>() {
@Override
public Integer apply(Integer i) {
return i + x;
}
};
f1.apply(1);
}
JDK 7
Variable ‘x’ is accessed from
within inner class.
Needs to be declared final
public static void main(String[] args) {
int x = 5; // Effectively final
Function<Integer, Integer> f1 =
new Function<Integer, Integer>() {
@Override
public Integer apply(Integer i) {
return i + x;
}
};
f1.apply(1);
}
JDK 8 – It compiles!
int x = 5; // effectively final
Function<Integer, Integer> f = i -> i + x;
f.apply(1); // 6
int x = 5; // not effectively final
Function<Integer, Integer> f = i-> i + ++x;
f.apply(1); // Does not compile
Effectively final
lambda == functional interface (SAM)
type depends on context
intuitive scoping, not like inner classes
enhanced type inference / effectively final variables
or
let the compiler work for you
method referencing for readability
numbers.forEach(System.out::println);
So where does this come from then?
Remember the first demo?
public interface Iterable<T> {
…
default void forEach(
Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
And while we’re at it, why
not add static methods as
well then…
Collection | Collections
Path | Paths
Static Interface Methods
@FunctionalInterface
public interface Comparator<T>
public static Comparator<T> reverseOrder() {
return Collections.reverseOrder();
}
public static Comparator<T> naturalOrder() {
return ...;
}
streams
1) Create
2) Intermediary operations
Stateless transformations
Stateful transformations
Extract / combine
3) Terminal operations
Reduction
Collecting
Stream API
numbers.stream();
1) Creating streams
Stream.of("stream", "of", "strings");
1) Call Collection.stream();
2) Use a static factory
3) Roll your own
public interface Spliterator<T>
List<Integer> numbers;
2) Transformations - stateful
Stream<String> chars =
Stream.of("A","B","D","A","B");
// { "A","B","D" }
Stream<String> distinctChars =
chars.distinct();
// {"A","A","B","B","D" };
Stream<String> sorted =
chars.sorted(); // default natural order
2) Transformations - stateless
Stream<String> words =
Stream.of("stream", "of", "strings");
// { "streams", "strings" }
Stream<String> longWords =
words.filter(s -> s.length() > 4);
// { 6, 2, 7 };
Stream<Integer> lengths =
words.map(s -> s.length());
3) Terminal operations
Stream<String> chars =
Stream.of("AB","CDE","FGHI");
long numberOfChars = chars.count(); // 3
// "FGHI"
Optional<String> max =
chars.max(comparing(String::length));
String street = "Unknown";
if (person != null
&& person.getAddress() != null
&& person.getAddress()
.getStreet() != null) {
street = person.getAddress().getStreet();
}
Optional values before JDK 8
Optional<Person> person;
String street =
person.map(Person::getAddress)
.map(Address::getStreet)
.orElse("Unknown");
Optional
// Creating a new Optional
Optional<String> value = Optional.of("Hi");
Optional<String> empty = Optional.empty();
// Most common operations
value.get(); // NoSuchElementException
value.orElse("something else");
value.ifPresent(v -> out.println(v));
value.isPresent();
Syntax
<R> R collect(
Supplier<R> supplier,
BiConsumer<R, ? super T> accumulator,
BiConsumer<R, R> combiner);
Collecting
Set<String> s = stream.collect(
HashSet::new,
HashSet::add,
HashSet::addAll
);
List<String> list =
stream.collect(Collectors.toList());
More convenient collecting
Set<String> set =
stream.collect(Collectors.toSet());
String joined =
stream.collect(joining(","));
{ live coding; }
NLJUG University Sessie: Java Reborn, Powered by Ordina
Problem Statement
// @since 1.5
public interface Future<V> {
boolean isDone();
V get()
V get(long timeout, TimeUnit unit)
}
java.util.concurrent.Future
ExecutorService es = newFixedThreadPool(2);
FutureTask<String> t1 = createTask("t1");
es.execute(t1); // and task 2
FutureTask<String> t3 =
createTask(t1.get() + t2.get());
es.execute(t3);
t3.get(1, SECONDS);
Old School Future
BlockingBlockingBlocking
Composition / composing asynchronous operations
Seeing the calculation as a chain of tasks
thenApply
thenAccept
run
thenCombine
thenAcceptBoth
runAfterBoth
...
Composition
Promise Pipelining
public interface CompletableFuture<T>
…
CompletableFuture thenCombine(
CompletableFuture other,
BiFunction combiner)
<U,V> CompletableFuture<V> thenCombine(
CompletableFuture<? extends U> other,
BiFunction<? super T,? super U,? extends V>
combiner)
CompletableFuture<String> task1 =
CompletableFuture.supplyAsync(
() -> doAction("t1"));
CompletableFuture<String> task3 =
(String p) -> supplyAsync(() -> doAction(p));
task1.thenCombine(task2, (r1, r2) -> r1 + r2)
.thenCompose(task3)
.exceptionally(t -> "UNKNOWN“)
.thenAccept(System.out::println)
String::concat)
CompletableFuture
Blocking
Martijn Blankestijn
martijn.blankestijn@ordina.nl
Remko de Jong
remko.de.jong@ordina.nl
? ? ? ? ?
What’s next ?
16:00 – 17:30
Presentation
17:30 – 18:15
Food
18:15 – 20:30
Lab
20:30 – 22:00
Drinks
USB-stick
• Exercises
• JDK 8
• mac, linux, windows, 32/64 bits
• javadoc
• IDE
• NetBeans 8
• IntelliJ Community Edition 13.1
Hands-on Lab: Rooms
A11.06 A11.02 A12.05 A12.06 A12.07
Martijn Philippe Ivo Pieter Remko

More Related Content

PDF
Java 8 features
PPT
Javascript
PPTX
PDF
JavaScript Programming
PDF
Clean coding-practices
PPTX
Java 8 presentation
PDF
PPTX
Java 8 Lambda and Streams
Java 8 features
Javascript
JavaScript Programming
Clean coding-practices
Java 8 presentation
Java 8 Lambda and Streams

What's hot (20)

PPT
PPTX
Lab #2: Introduction to Javascript
PDF
Lambda and Stream Master class - part 1
PPTX
Java 8 Feature Preview
PPTX
Java 8 lambda
PDF
Python Programming - VII. Customizing Classes and Operator Overloading
PPT
JavaScript Objects
PPT
Introduction to Javascript
PPT
Java tutorial for Beginners and Entry Level
PPTX
Java concurrency questions and answers
PDF
Java Class Design
PPTX
Java best practices
PDF
Python Programming - IX. On Randomness
PPT
Core java concepts
PPT
Core java by a introduction sandesh sharma
PPTX
Java generics final
PPT
Java Intro
PDF
Java 8 Lambda Expressions & Streams
PDF
Lombok Features
PDF
On Parameterised Types and Java Generics
Lab #2: Introduction to Javascript
Lambda and Stream Master class - part 1
Java 8 Feature Preview
Java 8 lambda
Python Programming - VII. Customizing Classes and Operator Overloading
JavaScript Objects
Introduction to Javascript
Java tutorial for Beginners and Entry Level
Java concurrency questions and answers
Java Class Design
Java best practices
Python Programming - IX. On Randomness
Core java concepts
Core java by a introduction sandesh sharma
Java generics final
Java Intro
Java 8 Lambda Expressions & Streams
Lombok Features
On Parameterised Types and Java Generics
Ad

Similar to NLJUG University Sessie: Java Reborn, Powered by Ordina (20)

PPTX
Introduction to Client-Side Javascript
ODP
Lambda Chops - Recipes for Simpler, More Expressive Code
PPTX
Java gets a closure
PPTX
New Features in JDK 8
PPT
An Overview Of Python With Functional Programming
PDF
FP in Java - Project Lambda and beyond
PPTX
Java-Intro.pptx
PPTX
Clean Code: Chapter 3 Function
PPT
Understanding linq
PPTX
Programming in java basics
PPTX
Java 8 new features
PDF
Java 8 Workshop
PDF
Charles Sharp: Java 8 Streams
PPT
PDF
Java 8 - Project Lambda
PDF
The Swift Compiler and Standard Library
PPT
Java Script Patterns
PPTX
Rx workshop
PPTX
Java8.part2
PPTX
What's new in Java 8
Introduction to Client-Side Javascript
Lambda Chops - Recipes for Simpler, More Expressive Code
Java gets a closure
New Features in JDK 8
An Overview Of Python With Functional Programming
FP in Java - Project Lambda and beyond
Java-Intro.pptx
Clean Code: Chapter 3 Function
Understanding linq
Programming in java basics
Java 8 new features
Java 8 Workshop
Charles Sharp: Java 8 Streams
Java 8 - Project Lambda
The Swift Compiler and Standard Library
Java Script Patterns
Rx workshop
Java8.part2
What's new in Java 8
Ad

Recently uploaded (20)

PPTX
MYSQL Presentation for SQL database connectivity
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
sap open course for s4hana steps from ECC to s4
PPT
Teaching material agriculture food technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
A Presentation on Artificial Intelligence
PDF
Electronic commerce courselecture one. Pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Big Data Technologies - Introduction.pptx
MYSQL Presentation for SQL database connectivity
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Digital-Transformation-Roadmap-for-Companies.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Programs and apps: productivity, graphics, security and other tools
Network Security Unit 5.pdf for BCA BBA.
sap open course for s4hana steps from ECC to s4
Teaching material agriculture food technology
Empathic Computing: Creating Shared Understanding
MIND Revenue Release Quarter 2 2025 Press Release
Diabetes mellitus diagnosis method based random forest with bat algorithm
A Presentation on Artificial Intelligence
Electronic commerce courselecture one. Pdf
Spectroscopy.pptx food analysis technology
Assigned Numbers - 2025 - Bluetooth® Document
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation_ Review paper, used for researhc scholars
Big Data Technologies - Introduction.pptx

NLJUG University Sessie: Java Reborn, Powered by Ordina