What are the new features of java-10?
On March 2018, Oracle released a new version of Java as Java-10. Following are the new features of java-10.
- Local Variable Type Inference:
- Previously we needed to mention the type of the local variable explicitly and it should be compatible with the initializer.
- For example, to declare and store string data we need to write as
- String str = “This is ugtworld.com”;
- But from java-10 we can declare the variable as follows:
- var str = “Hello, This is ugtworld.com”;
- We don’t provide the data type of str object. Instead, we mark the object as a var, and the compiler infers the type of object from the type of the initializer present on the right-hand side.
- Note that This feature is available only for local variables with the initializer. It cannot be used for member variables, method parameters, return types, array initializers, or lambda expressions. Else it will give the compile time error.
- This enhancement helps in reducing the boilerplate code. Example map object before java 10 is like below
- Map<Integer, String> map = new HashMap<>();
- we can create a map as shown in below from java-10 onwards,
- var map = new HashMap<Integer, String>();
package com.ugtworld;
import java.util.ArrayList;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
var str = “Hello, This is ugtworld.com”; // local variable
System.out.println(str);
var map = new HashMap<Integer, String>();
map.put(10, “ABCD”);
map.put(11, “EFGH”);
System.out.println(map);
var list = new ArrayList<String>();
list.add(“KJN”);
list.add(“DKS”);
list.add(“BBH”);
System.out.println(list);
}
}
import java.util.ArrayList;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
var str = “Hello, This is ugtworld.com”; // local variable
System.out.println(str);
var map = new HashMap<Integer, String>();
map.put(10, “ABCD”);
map.put(11, “EFGH”);
System.out.println(map);
var list = new ArrayList<String>();
list.add(“KJN”);
list.add(“DKS”);
list.add(“BBH”);
System.out.println(list);
}
}
Output:
Hello, This is ugtworld.com{10=ABCD, 11=EFGH}
[KJN, DKS, BBH]
- Time-Based Release Versioning:
- Starting with Java 10, Oracle has moved to the time-based release of Java.
- This means :
- A new Java release every six months.
- These are called feature releases and are expected to contain at least one or two significant features.
- Each update release is planned per quarter, Feature release will receive two Update releases before the next feature release is announced.
- Support for the feature release will last only for six months, i.e., until the next feature release.
- Long-Term Support(LTS) Release − LTS release will be announced after every three years starting from Sep 2018, Support for such release will be for three years.
- APIs for Creating Immutable Collections:
- To create Unmodifiable collection objects java10 introduced new APIs.
- The List.copyOf, Set.copyOf, and Map.copyOf methods create new collection instances from existing instances.
- New methods toUnmodifiableList, toUnmodifiableSet, and toUnmodifiableMap have been added to the Collectors class in the Stream package.
- If we try to add this new immutable collection instance we will get a runtime exception: java.lang.UnsupportedOperationException
package com.ugtworld;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
var list = new ArrayList<String>();
list.add(“KJN”);
list.add(“DKS”);
list.add(“BBH”);
list.add(“KMS”);
list.add(“KJM”);
list.add(“KJD”);
System.out.println(“List is: ” + list);
var list2 = List.copyOf(list);
System.out.println(“List2 is: ” + list2);
var list3 = list.stream()
.filter(i -> i.charAt(0) == ‘K’)
.collect(Collectors.toUnmodifiableList());
System.out.println(“List3 is: ” + list3);
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
var list = new ArrayList<String>();
list.add(“KJN”);
list.add(“DKS”);
list.add(“BBH”);
list.add(“KMS”);
list.add(“KJM”);
list.add(“KJD”);
System.out.println(“List is: ” + list);
var list2 = List.copyOf(list);
System.out.println(“List2 is: ” + list2);
var list3 = list.stream()
.filter(i -> i.charAt(0) == ‘K’)
.collect(Collectors.toUnmodifiableList());
System.out.println(“List3 is: ” + list3);
}
}
Output:
List is: [KJN, DKS, BBH, KMS, KJM, KJD]List2 is: [KJN, DKS, BBH, KMS, KJM, KJD]
List3 is: [KJN, KMS, KJM, KJD]
- Optional.orElseThrow() Method:
- A new method orElseThrow has been added to the Optional class.
- It is synonymous with and is now the preferred alternative to the existing get() method.
- This will throw NoSuchElementException if no value is present for mentioned condition.
package com.ugtworld;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
var list = Arrays.asList(1, 2, 5, 3, 4, 6, 5, 7, 8);
Integer firstEven = list.stream()
.filter(i -> i % 2 == 0)
.findFirst()
.orElseThrow();
System.out.println(firstEven);
// In the following case since there is no number > 10 we will get an exception
Integer max10 = list.stream()
.filter(i -> i > 10)
.findFirst()
.orElseThrow();
System.out.println(max10);
}
}
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
var list = Arrays.asList(1, 2, 5, 3, 4, 6, 5, 7, 8);
Integer firstEven = list.stream()
.filter(i -> i % 2 == 0)
.findFirst()
.orElseThrow();
System.out.println(firstEven);
// In the following case since there is no number > 10 we will get an exception
Integer max10 = list.stream()
.filter(i -> i > 10)
.findFirst()
.orElseThrow();
System.out.println(max10);
}
}
Output:
2Exception in thread “main” java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.orElseThrow (Optional.java:377)
at com.ugtworld.Test.main (Test.java:18)
- Parallel Full GC for G1:
- The G1 garbage collector is designed to avoid full collections, but when the concurrent collections can’t reclaim memory fast enough a fall back full GC will occur.
- The old implementation of the full GC for G1 used a single threaded mark-sweep-compact algorithm in java 10 full GC for G1 used a parallel mark-sweep-compact algorithm.
- Root Certificates:
For OpenJDK 9 binary for Linux x64 The empty cacerts keystore had prevented TLS connections from being established because Trusted Root Certificate Authorities were not installed so As a workaround for OpenJDK 9 binaries, users had to set the javax.net.ssl.trustStore System Property to use a different keystore.
From java 10 Provides a default set of root Certification Authority (CA) certificates in the JDK.
- Hashed Passwords for Out-of-the-Box JMX Agent:
- The clear passwords present in the jmxremote.password file are now being over-written with their SHA3-512 hash by the JMX agent.
- The hashed password follows the format:
- System Property to Disable JRE Last Usage Tracking
- A new system property jdk.disableLastUsageTracking has been introduced to disable JRE last usage tracking for a running VM.
- This property can be set in the command line by using either -Djdk.disableLastUsageTracking=true or -Djdk.disableLastUsageTracking.
- With this system property set, JRE last usage tracking will be disabled regardless of the com.oracle.usagetracker.track.last.usage property value set in usagetracker.properties.