1. Java 8
Good & Bad
Overview by Nicola Pedot - 26 giugno 2014
https://creativecommons.org/licenses/by/3.0/it
2. Status
On 2014, Java is one of the most used programming
language for client-server applications, with approximately
9 million developers.
Oracle Java Versions:
6 archived
7 production ready (java.net)
8 for developers (java.com)
4. Java 8 Parts
Process
Development Kit- JDK
Virtual Machine VM
Language
Runtime Libraries - JRE
Editions - JEE, JSE, JME, JEmbedded
5. Java Community Process
The JCP remains the governing body for all standard Java
SE APIs and related interfaces. If a proposal accepted into
this process intends to revise existing standard interfaces,
or to define new ones, then a parallel effort to design,
review, and approve those changes must be undertaken in
the JCP, either as part of a Maintenance Review of an
existing JSR or in the context of a new JSR.
6. JDK Enhancement-Proposal
JEP 1: JDK Enhancement-Proposal & Roadmap Process
Author Mark Reinhold
Organization Oracle
Created 2011/6/23
Updated 2012/12/4
The primary goal of this process is to produce a regularly-updated list of
proposals to serve as the long-term Roadmap for JDK Release Projects and related
efforts.
This process is open to every OpenJDK Committer.
This process does not in any way supplant the Java Community Process.
7. Java JDK - OpenJDK
JDK 8 was the second part of "Plan B". The single driving
feature of the release was Project Lambda. (Project Jigsaw
was initially proposed for this release but later dropped).
Additional features proposed via the JEP Process were
included so long as they fit into the overall schedule
required for Lambda. Detailed information on the features
included in the release can be found on the features page.
The source is open and avaliable on Mercurial
repository.
8. Java VM - HotSpot
Below you will find the source code for the Java HotSpot virtual
machine, the best Java virtual machine on the planet.
The HotSpot code base has been worked on by dozens of people,
over the course of 10 years, so far. (That's good and bad.) It's
big. There are nearly 1500 C/C++ header and source files,
comprising almost 250,000 lines of code. In addition to the
expected class loader, bytecode interpreter, and supporting
runtime routines, you get two runtime compilers from bytecode to
native instructions, 3 (or so) garbage collectors, and a set of
high-performance runtime libraries for synchronization, etc.
9. Java 8 Hot Topics
Default Methods
Function Iterfaces (Closure, Lambda) or AntiScala
Streams
Parallel
Javascript Nashorn
Java Time
SNI IPV6
Security
10. Default methods
Default methods enable you to add new
functionality to the interfaces of your libraries
and ensure binary compatibility with code
written for older versions of those interfaces.
Note: interfaces do not have any state
11. Default method syntax
public interface oldInterface {
public void existingMethod();
default public void newDefaultMethod() {
System.out.println("New default method"
" is added in interface");
}
}
The following class will compile successfully in Java JDK 8,?
public class oldInterfaceImpl implements oldInterface {
public void existingMethod() {
// existing implementation is here…
}
}
If you create an instance of oldInterfaceImpl:?
oldInterfaceImpl obj = new oldInterfaceImpl ();
// print “New default method add in interface”
obj.newDefaultMethod();
12. Default method conflict
java: class Impl inherits unrelated defaults for defaultMethod() from types InterfaceA and InterfaceB
In order to fix this class, we need to provide default method implementation:
public class Impl implements InterfaceA, InterfaceB {
public void defaultMethod(){
// existing code here..
InterfaceA.super.defaultMethod();
}
}
13. Default method good
The great thing about using interfaces instead
of adapter classes is the ability to extend
another class than the particular adapter.
Simil multiple inheritance.
Finally, library developers are able to evolve
established APIs without introducing
incompatibilities to their user's code.
14. Default method bad
In a nutshell, make sure to never override a default
method in another interface. Neither with another
default method, nor with an abstract method.
Before Java 7, you would only need to look for the
actually invoked code by traversing down a linear
class hierarchy. Only add this complexity when you
really feel it is absolutely necessary.
15. Lambda: functional interface
A functional interface is any interface that
contains only one abstract method. (A
functional interface may contain one or more
default methods or static methods.)
16. Lambda syntax
//Prima:
List list1 = Arrays.asList(1,2,3,5);
for(Integer n: list1) {
System.out.println(n);
}
//Dopo:
List list2 = Arrays.asList(1,2,3,5);
list2.forEach(n -> System.out.println(n));// default method forEach
//Espressioni lambda e doppio due punti static method reference
list2.forEach(System.out::println);
20. Stream Bad Parts
“Java 8 Streams API will be the single biggest source of
new Stack Overflow questions.”
With streams and functional thinking, we’ll run into a
massive amount of new, subtle bugs. Few of these bugs
can be prevented, except through practice and staying
focused. You have to think about how to order your
operations. You have to think about whether your streams
may be infinite. [14]
21. Stream Bad Parts (2)
“If evaluation of one parallel stream results in a very long running
task, this may be split into as many long running sub-tasks that
will be distributed to each thread in the pool. From there, no
other parallel stream can be processed because all threads will
be occupied.”
If a program is to be run inside a container, one must be very
careful when using parallel streams. Never use the default pool
in such a situation unless you know for sure that the container
can handle it. In a Java EE container, do not use parallel
streams. [15]
22. Parallel Lang Tools: StampedLock
The ReentrantReadWriteLock had a lot of shortcomings: It
suffered from starvation. You could not upgrade a read lock
into a write lock. There was no support for optimistic reads.
Programmers "in the know" mostly avoided using them.
StampedLock addresses all these shortcomings
23. Parallel Lang Tools:Concurrent Adders
Concurrent Adders:
this is a new set of classes for managing counters written
and read by multiple threads. The new API promises
significant performance gains, while still keeping things
simple and straightforward.
24. Bad Part
We’re paying the price for shorter, more
concise code with more complex debugging,
and longer synthetic call stacks.
25. The Reason
The reason is that while javac has been
extended to support Lambda functions, the
JVM still remains oblivious to them.
30. JavaScript with stream
DSL.using(conn)
.select(...) // this is folded code.
// This fetches a List<Map<String, Object>> as
// your ResultSet representation
.fetchMaps()
// This is Java 8's standard Collection.stream()
.stream()
// And now, r is like any other JavaScript object
// or record!
.forEach(function (r) {
print(r.TABLE_SCHEMA + '.'
+ r.TABLE_NAME + ' has '
+ r.CNT + ' columns.');
});
31. Javascript Problem
In this case the bytecode code is dynamically
generated at runtime using a nested tree of
Lambda expressions. There is very little
correlation between our source code, and the
resulting bytecode executed by the JVM. The
call stack is now two orders of magnitude
longer.
32. The Hard Side
Haskell is good at preventing bugs.
Java without lambda has readable stacktrace.
In Groovy is harder reading exceptions,
Java8 Lambda is also harder,
Javascript is even harder.
33. JavaTime, JodaTime’s revenge
● The Calendar class was not type safe.
● Because the classes were mutable, they could not be
used in multithreaded applications.
● Bugs in application code were common due to the
unusual numbering of months and the lack of type
safety.
34. JodaTime syntax
import java.time.Instant;
Instant timestamp = Instant.now();
This class format follows the ISO-8601
2013-05-30T23:38:23.085Z
Come gestire le vecchie date?
Date.toInstant()
public static Date from(Instant instant)
Calendar.toInstant()
Other classes Clock, Period,...
35. SNI IPV6
Assigning a separate IP address for each site increases the cost of hosting since requests for IP addresses must be justified to the
regional internet registry and IPv4 addresses are now in short supply.
An extension to TLS called Server Name Indication (SNI) addresses this issue by sending the name of the virtual domain as part
of the TLS negotiation. <<Wikipedia>>
E’ possibile configurare in un webserver più
virtual host con diversi certificati SSL
utilizzando un solo indirizzo IP.
provocazione...in vista dell’esaurimento di IP di
InternetOfThings…. Java Everywhere?
37. Others...
1. Process termination
Process destroyForcibly(); isAlive(); waitFor(long timeout, TimeUnit unit);
2. Optional Values
String name = computer.flatMap(Computer::getSoundcard)
.flatMap(Soundcard::getUSB)
.map(USB::getVersion)
.orElse("UNKNOWN");
3. Annotate Anything
Type Annotations are annotations that can be placed anywhere you use a type. This includes the new operator, type casts,
implements clauses and throws clauses
39. New Domains
Java started simple by design, now it has to
gain complexity to model new domains.
from Static Object Orienteed to
->(functional) Parallel Event Orienteed
->(dynamic) Syntax & Check relaxed
== More fun & more dangerous times ahead!
40. from Java8 to Java9
from….
Enterprise Edition
Standard Edition
Embedded Edition
Mobile Edition
…. to Java9 complete module system
41. Compact Profiles
Java Compact Profiles,
A reasonably configured Java SE-Embedded 8 for ARMv5/Linux
compact1 profile comes in at less than 14MB
compact2 is about 18MB and
compact3 is in the neighborhood of 21MB.
For reference, the Java SE-Embedded 7u21 Linux environment requires 45MB.