JDK 11 is the open-source reference implementation of version 11 of the Java SE Platform as specified by JSR 384 in the Java Community Process. The final release date 25 September 2018.
In this post, we are going to discuss the enhancement and new API in JDK 11 in brief. Let's categories them into two parts.
- Enhancement and new API changes that are visible to Developers.
- Enhancement and new API changes that are not visible to Developers.
Now let's focus on the first part and in the later posts, we will discuss the second part.
What are the enhancements in JDK 11?
JEP 323: Local-Variable Syntax for Lambda Parameters
- Type inference(JEP 286) was introduced in JDK 10 for local variables. After JDK 10, you don't need to explicitly state the type of a local-variable but you can use var. But this was not available for lambda's formal parameters.
- Before JDK 11
- From JDK 11
- As you already know that the lambda's formal parameters support type inference(don't need to define the type for formal parameters). But the question is why we are providing the var support for lambda's formal parameter?
- Because of only one special case that when you want to use annotations like @NotNull with lambda's formal parameters then you have to explicitly define the type of the formal parameter.
- But from JDK 11, you can use var and there is no need to define the type explicitly.
- The following examples are illegal:
- (var x, y) -> x.process(y) // Cannot mix 'var' and 'no var' in implicitly typed lambda expression
- (var x, int y) -> x.process(y) // Cannot mix 'var' and manifest types in explicitly typed lambda expression
JEP 321: HTTP Client (Standard)
- In JDK 9, the incubated HTTP Client API introduced(JEP 110), and updated in JDK 10.
- Incubator modules are a means of putting non-final APIs and non-final tools in the hands of developers, while the APIs/tools progress towards either finalization or removal in a future release
- Now, the HTTP Client API is part of Java SE 11.
- The module name and the package name of the standard API will be java.net.http.
- HttpClient
- HttpRequest
- HttpResponse
- WebSocket
- The API can be used asynchronously. The asynchronous mode makes use of CompletableFutures and CompletionStages.
JEP 320: Remove The Java EE and CORBA Modules
- Remove the Java EE and CORBA modules from the Java SE Platform and the JDK. These modules were deprecated in Java SE 9 with the declared intent to remove them in a future release.
API Changes:
- java.io.ByteArrayOutputStream
- A new method public void writeBytes(byte b[]) is added.
- Writes the complete contents of the specified byte array to an output stream. This method is equivalent to public synchronized void write(byte b[], int off, int len).
- java.lang.Character
- A new method public static String toString(int codePoint) is added.
- java.lang.CharSequence
- A new method public static int compare(CharSequence cs1, CharSequence cs2) is added.
- Compares two CharSequence instances lexicographically. Returns a negative value, zero, or a positive value if the first sequence is lexicographically less than, equal to, or greater than the second, respectively.
- java.lang.ref.Reference
- A new method protected Object clone() throws CloneNotSupportedException is added.
- Throws CloneNotSupportedException. A Reference cannot be meaningfully cloned. Construct a new Reference instead.
- java.lang.String
- public boolean isBlank()
- This new method is added but we already have isEmpty() then why this new method is added?
- For reason, consider the below example
System.out.println(s.isBlank());
System.out.println(s.isEmpty());
- public String repeat(int count)
- Returns a string whose value is the concatenation of this string repeated count times where the count is the no. of time the string will be repeated.
- public String strip()
- Returns a string whose value is this string, with all leading and trailing white space removed.
- public String stripLeading()
- Returns a string whose value is this string, with all leading white space removed.
- public String stripTrailing()
- Returns a string whose value is this string, with all trailing white space removed.
- public Stream<String> lines()
- Returns a stream of lines extracted from this string, separated by line terminators.
- A line terminator can be one of these "\n", "\r", "\r\n".
- java.lang.StringBuffer and java.lang.StringBuilder
- public synchronized int compareTo(StringBuffer another) inside StringBuffer
- public int compareTo(StringBuilder another) inside StringBuilder
- From JDK 11, there is no ClassCastException will occur while adding StringBuffer or StringBuilder object inside the TreeSet or TreeMap.
- java.util.function.Predicate
- static <T> Predicate<T> not(Predicate<? super T> target)
- Returns a predicate that is the negation of the supplied predicate.


Comments
Post a Comment