Skip to main content

Enhancement & New API in JDK 11

 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                        
                            String s = null; // in both case null pointer.             String s = ""; // in both case true.             String s = " "; // isBlank()-->true and isEmpty()-->false.             String s = "example"; // in both case false.             String s = " example "; // in both case false.
            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.
Thank you, guys. Please do comment below if there is any query or feedback.

Comments

Popular posts from this blog

How to add an existing project to a GitHub Repository

  Add Existing project to a GitHub Repository Step 1: Login into your GitHub account and create a new repository. Step 2: Go to your project folder on your machine and open git bash. After that runs the command “ git init ”. This command initialized the empty git repository on your local machine. Step 3: Create a file “. gitignore ” using the command “ echo > .gitignore ”. Add the file extension or file name inside this file, those you don’t want to commit to your repository. Git will ignore all those files that will be present inside this file. Step 4: Add all the files to the staging area using the command “git add .” This command adds all the files to the staging area and all these files are now ready for the commit. And with the help of the command “git status” , you can check that all the files are inside the staging area. Also, you can see the file extension or the file name mentioned inside the “.gitignore”  are ignored by git. Step 5: Commit the files that are pre...

Lambda Expression

What is Lambda Expression? In java 8, a powerful concept is added named Lambda Expression.  Lambda expression is an anonymous function (a nameless function that does not have a modifier, return type, and even the type of its arguments).  Lambda expression is that you can pass to a method whose argument type is functional interface i.e. an interface that is either annotated by @FunctionalInterface  annotation e.g. interfaces from java.util.function package, or an interface with a single abstract method e.g. Comparable, Comparator, Runnable, Callable, ActionListener, etc.  Lambda expression allows passing customized code to the java method that was not possible before java 8. Although before Java 8 the only option was to wrap your code as an object and pass it using anonymous inner class to the method. Lambda expression defines parameters and code that executed using those parameters. Let's take the simplest example of the lambda expression (String input) -> ...

How to install IntelliJ IDEA on Windows?

System Requirements and steps to install IntelliJ IDEA on Windows System Requirements:  Step 1: Download IntelliJ IDEA. Click here. Step 2: Run the downloaded exe file and click on next. Step 3: Check the checkboxes mentioned in the below image and click on next and then hit the install button. Step 4: Final step is to reboot your system before start using IntelliJ IDEA. Thank you, guys. Please do comment below if there is any query or feedback.