2. WHAT IS NEW IN JAVA 10
Java 10 contains various new features and enhancements.
This is the fastest release of a java version in its 23 year
history :)
1. New Features in Language
2. New Features in Compiler
3. New Features in Libraries
4. New Features in Tools
5. New Features in Runtime
6. Miscellaneous Changes
2
3. WHAT IS NEW IN JAVA 10
1. NEW FEATURES IN LANGUAGE
1. Local variable type inference
2. Time-Based Release Versioning
3. Root Certificates
3
4. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN LANGUAGE
1.1 LOCAL VARIABLE TYPE INFERENCE
‣ Java is known to be a bit verbose, which can be good when it comes to
understanding what you or another developer had in mind when a function was
written. Local variable type inference feature breaks this rule.
‣ Local variable type inference is the biggest new feature in Java 10 for developers.
‣ With the exception of assert from the Java 1.4 days, new keywords always seem
to make a big splash, and var is no different.
‣ What the var keyword does is turn local variable assignments:
Map<String, String> myMap = new HashMap<>();
into:
var thisIsAlsoMyMap = new HashMap<String, String>();
4
5. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN LANGUAGE
1.1 LOCAL VARIABLE TYPE INFERENCE
‣ Local type inference can be used only in the following
scenarios:
- Limited only to local variable with initializer
- Indexes of enhanced for loop or indexes
- Local declared in for loop
‣ It cannot be used for member variables, method parameters,
return types, etc.
‣ var is not a keyword – this ensures backward compatibility for
programs using var as a function or variable name.
5
6. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN LANGUAGE
1.1 LOCAL VARIABLE TYPE INFERENCE
‣ We can’t use ‘var’ in scenarios below:
- var n; // cannot use 'var' without initializer
- var emptyList = null; // variable initializer is ‘null'
- public var = "hello"; // 'var' is not allowed here
- var p = (String s) -> s.length() > 10; // lambda expression
needs an explicit target-type
- var arr = { 1, 2, 3 }; // array initializer needs an explicit
target-type
6
7. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN LANGUAGE
1.1 LOCAL VARIABLE TYPE INFERENCE
‣ Example:
var numbers = List.of(1, 2, 3, 4, 5); // inferred value ArrayList<String>
// Index of Enhanced For Loop
for (var number : numbers) {
System.out.println(number);
}
// Local variable declared in a loop
for (var i = 0; i < numbers.size(); i++) {
System.out.println(numbers.get(i));
}
7
8. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN LANGUAGE
1.2 TIME-BASED RELEASE VERSIONING
‣ The development of new Java versions was, up until now,
very feature driven.
‣ This meant that you had to wait for a few years for the next
release.
‣ Oracle has now switched to a new, time based model.
‣ Not everyone agrees with this proceeding. Larger
companies also appreciated the stability and the low rate
of change of Java so far
8
9. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN LANGUAGE
1.2 TIME-BASED RELEASE VERSIONING
‣ Oracle has responded to these concerns and continues to
offer long-term releases on a regular basis, but also at
longer intervals. And after Java 8, it is Java 11, which will
receive a long term support again.
‣ Java 9 and Java 10 on the other hand will only be
supported for the time period of half a year, until the next
release is due.
‣ In fact, Java 9 and Java 10 support has just ended, since
Java 11 is out.
9
10. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN LANGUAGE
1.2 TIME-BASED RELEASE VERSIONING
‣ With adoption of time based release cycle, Oracle changed the version-string
scheme of the Java SE Platform and the JDK, and related versioning
information, for present and future time-based release models
‣ The new pattern of the Version number is: FEATURE.INTERIM.UPDATE.PATCH
‣ FEATURE: counter will be incremented every 6 months and will be based on
feature release versions, e.g: JDK 10, JDK 11.
‣ INTERIM: counter will be incremented for non-feature releases that contain
compatible bug fixes and enhancements but no incompatible changes.
‣ UPDATE: counter will be incremented for compatible update releases that fix
security issues, regressions, and bugs in newer features.
‣ PATCH: counter will be incremented for an emergency release to fix a critical
issue.
10
11. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN LANGUAGE
1.3 ROOT CERTIFICATES
‣ The cacerts keystore, which was initially empty so far, is
intended to contain a set of root certificates that can be used to
establish trust in the certificate chains used by various security
protocols.
‣ With Java 10, Oracle has open-sourced the root certificates in
Oracle’s Java SE Root CA program in order to make OpenJDK
builds more attractive to developers and to reduce the
differences between those builds and Oracle JDK builds.
‣ Basically, this means that now doing simple things like
communicating over HTTPS between your application and, say,
a Google RESTful service will be much simpler with OpenJDK.
11
12. WHAT IS NEW IN JAVA 10
2. NEW FEATURES IN COMPILER
1. Experimental Java-Based JIT Compiler
2. Bytecode Generation for Enhanced for Loop
12
13. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN COMPILER
2.1 EXPERIMENTAL JAVA-BASED JIT COMPILER
‣ The Just-In-Time (JIT) Compiler is the part of java that converts java
byte code into machine code at runtime. It was written in c++
‣ This feature enables the Java-based JIT compiler, Graal, to be used as
an experimental JIT compiler on the Linux/x64 platform.
‣ Graal is a complete rewrite of the JIT compiler in java from scratch.
‣ To enable Graal, add these flags to your command line arguments
when starting the application:
-XX:+UnlockExperimentalVMOptions -XX:+UseJVMCICompiler
‣ Keep in mind that the Graal team makes no promises in this first release
that this compiler is any faster.
13
14. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN COMPILER
2.2 BYTE CODE GENERATION FOR ENHANCED FOR LOOP
‣ Bytecode generation has been improved for enhanced for loops, providing
an improvement in the translation approach for them. For example:
List<String> data = new ArrayList<>(); for (String b : data);
The following is the code generated after the enhancement:
{ /*synthetic*/ Iterator i$ = data.iterator(); for (; i$.hasNext(); ) { String b =
(String)i$.next(); } b = null; i$ = null; }
‣ Declaring the iterator variable outside of the for loop allows a null to be
assigned to it as soon as it is no longer used
‣ This makes it accessible to the GC, which can then get rid of the unused
memory.
14
15. WHAT IS NEW IN JAVA 10
3. NEW FEATURES IN LIBRARIES
1. Creating Unmodifiable Collections
2. Optional.orElseThrow() Method
15
16. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN LIBRARIES
3.1 CREATING UNMODIFIABLE COLLECTIONS
1. Several new APIs have been added that facilitate the creation of unmodifiable
collections.
2. The List.copyOf, Set.copyOf, and Map.copyOf methods create new collection
instances from existing instances.
3. New methods toUnmodifiableList, toUnmodifiableSet, and
toUnmodifiableMap have been added to the Collectors class in the stream
package. These methods allow the elements of a Stream to be collected into
an unmodifiable collection.
Stream<String> myStream = Stream.of("a", "b", "c");
List<String> unModifiableList =
myStream.collect(Collectors.toUnmodifiableList());
unModifiableList.add(“d"); // throws java.lang.UnsupportedOperationException
16
17. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN LIBRARIES
3.2 CREATING UNMODIFIABLE COLLECTIONS
‣ Optional, OptionalDouble, OptionalInt and OptionalLong each
got a new method orElseThrow() which doesn’t take any argument
and throws NoSuchElementException if no value is present:
@Test
public void whenListContainsInteger_OrElseThrowReturnsInteger() {
Integer firstEven = someIntList.stream()
.filter(i -> i % 2 == 0)
.findFirst()
.orElseThrow();
is(firstEven).equals(Integer.valueOf(2));
}
17
18. WHAT IS NEW IN JAVA 10
4. NEW FEATURES IN TOOLS
1. JShell Startup
2. Removed Tools
18
19. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN TOOLS
4.1 JSHELL STARTUP
‣ The time needed to start JShell has been significantly
reduced, especially in cases where a start file with many
snippets is used
19
20. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN TOOLS
4.2 REMOVED TOOLS
‣ Tool javah has been removed from Java 10 which
generated C headers and source files which were required
to implement native methods. Now, javac -h can be used
instead.
‣ policytool was the security tool for policy file creation and
management. This has now been removed.
20
21. WHAT IS NEW IN JAVA 10
5. NEW FEATURES IN RUNTIME
1. Parallel Full GC for G1
2. Improvements for Docker Containers
3. Application Data-Class Sharing
4. Removed Options
21
22. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN RUNTIME
5.1. PARALLEL FULL GC FOR G1
‣ G1 garbage collector was made default in JDK 9.
‣ However, the full GC for G1 used a single threaded mark-sweep-
compact algorithm.
‣ This has been changed to the parallel mark-sweep-compact algorithm
in Java 10 effectively reducing the stop-the-world time during full GC.
‣ This change won’t help the best-case performance times of the
garbage collector, but it does significantly reduce the worst-case
latencies.
‣ When concurrent garbage collection falls behind, it triggers a Full GC
collection.
22
23. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN RUNTIME
5.2. IMPROVEMENTS FOR DOCKER CONTAINERS
‣ The JVM now knows when it is running inside a Docker
Container.
‣ This means the application now has accurate information
about what the docker container allocates to memory,
CPU, and other system resources.
‣ Previously, the JVM queried the host operating system to
get this information.
‣ However, this support is only available for Linux-based
platforms.
23
24. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN RUNTIME
5.2. IMPROVEMENTS FOR DOCKER CONTAINERS
‣ There are command line options to specify how the JVM inside a Docker container allocates
internal memory.
‣ This new support is enabled by default and can be disabled in the command line with the
JVM option:
-XX:-UseContainerSupport
‣ To set the memory heap to the container group size and limit the number of processors you
could pass in these arguments:
-XX:+UseCGroupMemoryLimitForHeap -XX:ActiveProcessorCount=2
‣ Three new JVM options have been added to allow Docker container users to gain more fine-
grained control over the amount of system memory that will be used for the Java Heap:
-XX:InitialRAMPercentage
-XX:MaxRAMPercentage
-XX:MinRAMPercentage
24
25. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN RUNTIME
5.3. APPLICATION DATA-CLASS SHARING
‣ Java 5 introduced Class-Data Sharing (CDS) to improve startup times of
smaller Java applications.
‣ CDS only allowed the bootstrap class loader, limiting the feature to system
classes only.
‣ This feature extends the existing CDS feature for allowing application classes
to be placed in the shared archive in order to improve startup and footprint.
‣ The general idea was that when the JVM first launched, anything loaded was
serialized and stored in a file on disk that could be reloaded on future
launches of the JVM.
‣ This meant that multiple instances of the JVM shared the class metadata so it
wouldn’t have to load them all every time.
25
26. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN RUNTIME
5.3. APPLICATION DATA-CLASS SHARING
‣ We can use the following steps to make use of this feature:
1. Get the list of classes to archive:
$ java -Xshare:off -XX:+UseAppCDS
-XX:DumpLoadedClassList=hello.lst -cp hello.jar HelloWorld
2. Create the AppCDS archive:
$ java -Xshare:dump -XX:+UseAppCDS
-XX:SharedClassListFile=hello.lst
-XX:SharedArchiveFile=hello.jsa -cp hello.jar
3. Use the AppCDS archive:
$ java -Xshare:on -XX:+UseAppCDS
-XX:SharedArchiveFile=hello.jsa -cp hello.jar HelloWorld
26
27. WHAT IS NEW IN JAVA 10 - NEW FEATURES IN RUNTIME
5.4. REMOVED OPTIONS
‣ Removal of FlatProfiler: The FlatProfiler, deprecated in
JDK 9, has been made obsolete by removing the
implementation code. The FlatProfiler was enabled by
setting the -Xprof VM argument. The -Xprof flag remains
recognized in this release; however, setting it will print out
a warning message
‣ Removal of Obsolete -X Options: The obsolete HotSpot
VM options (-Xoss, -Xsqnopause, -Xoptimize, -
Xboundthreads, and -Xusealtsigs) have been removed
27
28. WHAT IS NEW IN JAVA 10
6. MISCELLANEOUS CHANGES
‣ Class File Version Number is 54.0:
The class file version has been changed from 53 (or 44 + 9) to 54 (44 +10), even though
JDK 10 did not introduce other changes to the class file format.
‣ Consolidate the JDK Forest into a Single Repository:
Combined the numerous repositories of the JDK forest into a single repository to
simplify and streamline development. The code base until now has been broken into
multiple repos, which can cause problems with source-code management.
‣ Additional Unicode Language-Tag Extensions:
Enhanced the java.util.Locale and related APIs to implement additional Unicode
extensions of BCP 47 language tags.
‣ Garbage Collector Interface:
A clean garbage collector interface to improve source-code isolation of different
garbage collectors. The goals for this effort include better modularity for internal
garbage collection code in the HotSpot virtual machine and making it easier to add a
new garbage collector to HotSpot.
28
29. WHAT IS NEW IN JAVA 10
6. MISCELLANEOUS CHANGES
‣ Heap Allocation on Alternative Memory Devices:
It enables the HotSpot VM to allocate the Java object heap
on an alternative memory device, such as an NV-DIMM,
specified by the user.
‣ Thread-Local Handshakes
This is an internal JVM feature to improve performance.
This feature provides a way to execute a callback on
threads without performing a global VM safepoint. Make it
both possible and cheap to stop individual threads and
not just all threads or none.
29
30. WHAT IS NEW IN JAVA 10
SOURCES
- https://www.oracle.com/technetwork/java/javase/10-
relnote-issues-4108729.html
- http://cr.openjdk.java.net/~iris/se/10/latestSpec/
- https://www.baeldung.com/java-10-overview
- https://stackify.com/whats-new-in-java-10/
- https://www.journaldev.com/20395/java-10-features
- https://www.quora.com/What-is-new-in-Java-10
30