Java 11 Features

Java 11 Features:




  1. New Collection.toArray(IntFunction) Default Method


A new default method toArray(IntFunction) has been added to the java.util.Collection interface. This method allows the collection's elements to be transferred to a newly created array of a desired runtime type. The new method is an overload of the existing toArray(T[]) method that takes an array instance as an argument. T


  1. Running Java File with single command

      One major change is that you don’t need to compile the java source file with javac tool first. You can directly run the file with java command and it implicitly compiles.


  1. Java String Methods

isBlank() – This instance method returns a boolean value. Empty Strings and Strings with only white spaces are treated as blank.

lines()- This method returns a stream of strings, which is a collection of all substrings split by lines. eg: str.lines().collect(Collectors.toList())

strip(), stripLeading(), stripTrailing()

repeat(int):  The repeat method simply repeats the string that many numbers of times as mentioned in the method in the form of an int. eg: String str = "=".repeat(2); // o/p: =


  1. Local-Variable Syntax for Lambda Parameters

JEP 323 allows var to be used to declare the formal parameters of an implicitly typed lambda expression.

We can now define : (var s1, var s2) -> s1 + s2


  1. Nested Based Access Control


Java 11 nested access control addresses this concern in reflection.

java.lang.Class introduces three methods in the reflection API: getNestHost(), getNestMembers(), and isNestmateOf().


  1. Reading/Writing Strings to and from the Files


Java 11 strives to make reading and writing of String convenient.
It has introduced the following methods for reading and writing to/from the files:

    • readString()
    • writeString()


  1. Lazy Allocation of Compiler Threads

A new command line flag -XX:+UseDynamicNumberOfCompilerThreads has been added to dynamically control compiler threads.


  1. Z Garbage Collector:
    • Scalable low latency
    • Concurrent collector
    • Experimental

  

  1. Epsilon  Garbage Collector:
    • Its No Operation GC
    • It just collected the data but does not reclaims


  1. Predicate     negate = Predicate.not( positivePredicate );
  2. JDK Flight Recorder Its a diagnostic tool like black box for aircrafts


Local variable with lambda parameters


Test t = (var s1, var s2) -> s1 + s2; // valid with var 

Test t2 = (int s1, int s2) -> s1 + s2; // valid 

Test t3 = (s1, s2) -> s1 + s2; // valid 

Test t4 = (int s1, var s2) -> s1 + s2; // mixing not valid

t.testing(10, 20);



Nested control:


public class Main {

 

    public void myPublic() {

    }

 

    private void myPrivate() {

    }

 

    class Nested {

 

        public void nestedPublic() {

            myPrivate();

        }

    }

}


private method of the main class is accessible from the above-nested class in the above manner.

But if we use Java Reflection before java 11  it will give an IllegalStateException.


Method method = ob.getClass().getDeclaredMethod("myPrivate");

method.invoke(ob);


Java 11 nested access control addresses this concern in reflection.
java.lang.Class introduces three methods in the reflection API: 

getNestHost(), 

getNestMembers(), and 

isNestmateOf().



Dynamic Constants:


https://www.javacodegeeks.com/2018/08/hands-on-java-constantdynamic.html



String methods strip(), stripLeading(), stripTrailing()


String s = " sumith  ";

System.out.println(s.strip());

System.out.println(s.stripLeading());

System.out.println(s.stripTrailing());



HTTP Client:



HttpClient httpClient = HttpClient.newBuilder()

        .connectTimeout(Duration.ofSeconds(10))

        .build();


try

{

    String urlEndpoint = "https://postman-echo.com/get";

    URI uri = URI.create(urlEndpoint + "?foo1=bar1&foo2=bar2");

    HttpRequest request = HttpRequest.newBuilder()

            .uri(uri)

            .build();

    HttpResponse<String> response = httpClient.send(request,

            HttpResponse.BodyHandlers.ofString());

    System.out.println("Status code: " + response.statusCode());

    System.out.println("Headers: " + response.headers().allValues("content-type"));

    System.out.println("Body: " + response.body());

} catch (IOException | InterruptedException e) {

    throw new RuntimeException(e);

}



New Collection.toArray(IntFunction) Default Method

In Java 11, a new default method, toArray(IntFunction), has been added to the java.util.Collection interface, which allows the collection’s elements to be transferred to a newly created array of a desired runtime type.


default <T> T[] toArray​(IntFunction<T[]> generator)


Previously : 

String[] array = list.toArray(new String[list.size()]);


New:

Eg: String[] y = x.toArray(String[]::new);

Integer[] array2 = hset1.toArray(Integer[]::new);


Z Garbage Collector:


  • Z garbage collector threads runs along with the application threads.
  • When garbage collector runs , it can pause the application and max pause time is 10 ms. 
  • Max pause time does not increase with the size of the garbage recollection memory size it can be terabytes. 
  • Only available in linux 64 bit platform
  • It concurrent collector


Technique applied in GC


Every object stored in the GC stores the object metadata. 



Epsilon  Garbage Collector:


Epsilon GC is no Operation GC

It just notifies but does not reclaims


Usage

  • Performance testing
  • Memory pressure testing
  • VM interface testing
  • Extremely short lived jobs
  • Last-drop latency improvements
  • Last-drop throughput improvements





Comments

Popular posts from this blog

Java 12 Features

Java 13 features