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
1. NEW FEATURES IN LANGUAGE
1. Local variable type inference
2. Time-Based Release Versioning
3. Root Certificates
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>();
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.
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
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));
}
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
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.
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.
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.
2. NEW FEATURES IN COMPILER
1. Experimental Java-Based JIT Compiler
2. Bytecode Generation for Enhanced for Loop
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.
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.
3. NEW FEATURES IN LIBRARIES
1. Creating Unmodifiable Collections
2. Optional.orElseThrow() Method
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
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));
}
4. NEW FEATURES IN TOOLS
1. JShell Startup
2. Removed 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
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 usedinstead.
‣ policytool was the security tool for policy file creation and management. This has now been removed.
5. NEW FEATURES IN RUNTIME
1. Parallel Full GC for G1
2. Improvements for Docker Containers
3. Application Data-Class Sharing
4. Removed Options
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.
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.
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
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.
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
0 Comments
Share