New Features Haim Michael June 4th , 2019 All logos, trade marks and brand names used in this presentation belong to the respective owners. lifemichael Java 11 1st Part https://youtu.be/zggphqAcUvw 2nd Part https://youtu.be/ViL0flSGrGY
© 1996-2018 All Rights Reserved. Haim Michael Introduction ● Snowboarding. Learning. Coding. Teaching. More than 20 years of Practical Experience. lifemichael
© 1996-2018 All Rights Reserved. Haim Michael Introduction ● Professional Certifications Zend Certified Engineer in PHP Certified Java Professional Certified Java EE Web Component Developer OMG Certified UML Professional ● MBA (cum laude) from Tel-Aviv University Information Systems Management lifemichael
© 2008 Haim Michael 20151026 Oracle JDK is not Free  As of Java 11, the Oracle JDK would no longer be free for commercial use.  The Open JDK continues to be free. We can use it instead. However, we won't get nor security updates and nor any update at all.
© 2008 Haim Michael 20151026 Running Java File  We can avoid the compilation phase. We can compile and execute in one command. We use the java command. It will implicitly compile without saving the .class file.
© 2008 Haim Michael 20151026 Running Java File
© 2008 Haim Michael 20151026 Java String Methods  The isBlank() method checks whether the string is an empty string, meaning... whether the string solely includes zero or more blanks. String with only white characters is treated as a blank string. public class Program { public static void main(String args[]) { var a = ""; var b = " "; System.out.println(a.isBlank()); System.out.println(b.isBlank()); } }
© 2008 Haim Michael 20151026 Java String Methods  The lines() method returns a reference for a stream of strings that are substrings we received after splitting by lines. public class Program { public static void main(String args[]) { var a = "wenlovenjavanandnkotlin"; var stream = a.lines(); stream.forEach(str->System.out.println(str)); } }
© 2008 Haim Michael 20151026 Java String Methods  The strip(), stripLeading()and the stripTrailing methods remove white spaces from the beginning, the ending and the remr of the string. It is a 'Unicode-Aware' evolution of trim(); public class Program { public static void main(String args[]) { var a = " we love kotlin "; var b = a.strip(); System.out.println("###"+b+"###"); } }
© 2008 Haim Michael 20151026 Java String Methods  The repeat() method repeats the string on which it is invoked the number of times it receives. public class Program { public static void main(String args[]) { var a = "love "; var b = a.repeat(2); System.out.println(b); } }
© 2008 Haim Michael 20151026 Using var in Lambda Expressions  As of Java 11 we can use the var keyword within lambda expressions. interface Calculation { public int calc(int a,int b); } public class Program { public static void main(String args[]) { Calculation f = (var a, var b)-> a+b; System.out.println(f.calc(4,5)); } }
© 2008 Haim Michael 20151026 Using var in Lambda Expressions  When using var in a lambda expression we must use it on all parameters and we cannot mix it with using specific types.
© 2008 Haim Michael 20151026 Inner Classes Access Control  Code written in methods defined inside inner class can access members of the outer class, even if these members are private. class Outer { private void a() {} class Inner { void b() { a(); } } }
© 2008 Haim Michael 20151026 Inner Classes Access Control  As of Java 11, there are new methods in Class class that assist us with getting information about the created nest. These methods include the following: getNestHost(), getNestMembers() and isNestemateOf().
© 2008 Haim Michael 20151026 Epsilon  As of Java 11, the JVM has an experimental feature that allows us to run the JVM without any actual memory reclamation.  The goal is to provide a completely passive garbage collector implementation with a bounded allocation limit and the lowest latency overhead possible. https://openjdk.java.net/jeps/318
© 2008 Haim Michael 20151026 Deprecated Modules Removal  As of Java 11, Java EE and CORBA modules that were already marked as deprecated in Java 9 are now completely removed. java.xml.ws java.xml.bind java.activation java.xml.ws.annotation java.corba java.transaction java.se.ee jdk.xml.ws jdk.xml.bind
© 2008 Haim Michael 20151026 Flight Recorder  The Flight Recorder is a profiling tool with a negligible overhead below 1%. This extra ordinary overhead allows us to use it even in production.  The Flight Recorder, also known as JFR, used to be a commercial add-on in Oracle JDK. It was recently open sourced.
© 2008 Haim Michael 20151026 Flight Recorder  Using the jps command we can get the process id of our Java program.
© 2008 Haim Michael 20151026 Flight Recorder  Using the jcmd command we can perform various commands, such as jcmd 43659 JFR.start
© 2008 Haim Michael 20151026 Flight Recorder  Using the jcmd command together with JFR.dump we can dump all data to a textual file we choose its name and it location on our computer.
© 2008 Haim Michael 20151026 Flight Recorder  There are various possibilities to process the new created data file. https://github.com/lhotari/jfr-report-tool
© 2008 Haim Michael 20151026 The HTTP Client  As of Java 11, the HTTP Client API is more standardized.  The new API supports both HTTP/1.1 and HTTP/2.  The new API also supports HTML5 WebSockets.
© 2008 Haim Michael 20151026 The HTTP Client  As of Java 11, the HTTP Client API is more standardized. The new API supports both HTTP/1.1 and HTTP/2. The new API also supports HTML5 WebSockets.
© 2008 Haim Michael 20151026 The HTTP Client package com.lifemichael.samples; import java.io.IOException; import java.net.http.*; import java.net.*; import java.net.http.HttpResponse.*; public class HTTPClientDemo { public static void main(String args[]) { HttpClient httpClient = HttpClient.newBuilder().build();
© 2008 Haim Michael 20151026 The HTTP Client HttpRequest request = HttpRequest.newBuilder().uri(URI.create( "http://www.abelski.com/courses/dom/lib_doc.xml")) .GET() .build(); try { HttpResponse<String> response = httpClient.send( request, BodyHandlers.ofString()); System.out.println(response.body()); System.out.println(response.statusCode()); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
© 2008 Haim Michael 20151026 The HTTP Client  The new HTTP Client allows us to initiate an asynchronous HTTP request. HttpClient .sendAsync(request,BodyHandlers.ofString()) .thenAccept(response -> { System.out.println(response.body()); //.. });
© 2008 Haim Michael 20151026 The HTTP Client  The new HTTP Client supports the WebSocket protocol. HttpClient httpClient = HttpClient.newBuilder() .executor(executor).build(); Builder webSocketBuilder = httpClient.newWebSocketBuilder(); WebSocket webSocket = webSocketBuilder .buildAsync( URI.create("wss://echo.websocket.org"), … ).join();
© 2008 Haim Michael 20151026 Nashorn is Deprecated  As of Java 11, the Nashorn JavaScript engine and APIs are deprecated. Most likely, in future versions of the JDK.
© 2008 Haim Michael 20151026 The ZGC Scalable Low Latency GC  As of Java 11, we can use the ZGC. This new GC is available as an experimental feature.  ZGC is a scalable low latency garbage collector. It performs the expensive work concurrently without stopping the execution of application threads for more than 10ms.
© 2008 Haim Michael 20151026 The ZGC Scalable Low Latency GC  ZGC is suitable especially for applications that require low latency and/or use a very large heap (multi- terabytes).
© 2008 Haim Michael 20151026 Files Reading and Writing  Java 11 introduces two new methods that significantly assist with reading and writing strings from and to files. readString() writeString()
© 2008 Haim Michael 20151026 Files Reading and Writing public class FilesReadingDemo { public static void main(String args[]) { try { Path path = FileSystems.getDefault() .getPath(".", "welove.txt"); String str = Files.readString(path); System.out.println(str); } catch (IOException e) { e.printStackTrace(); } }
© 2008 Haim Michael 20151026 Files Reading and Writing public class FilesWritingDemo { public static void main(String args[]) { Path path = FileSystems.getDefault() .getPath(".", "welove.txt"); try { Files.writeString(path, "we love php", StandardOpenOption.APPEND); } catch (IOException e) { e.printStackTrace(); } } }
© 2008 Haim Michael 20151026 Q&A Thanks for attending our meetup! I hope you enjoyed! I will be more than happy to get feedback. Haim Michael haim.michael@lifemichael.com 0546655837

Java11 New Features

  • 1.
    New Features Haim Michael June4th , 2019 All logos, trade marks and brand names used in this presentation belong to the respective owners. lifemichael Java 11 1st Part https://youtu.be/zggphqAcUvw 2nd Part https://youtu.be/ViL0flSGrGY
  • 2.
    © 1996-2018 AllRights Reserved. Haim Michael Introduction ● Snowboarding. Learning. Coding. Teaching. More than 20 years of Practical Experience. lifemichael
  • 3.
    © 1996-2018 AllRights Reserved. Haim Michael Introduction ● Professional Certifications Zend Certified Engineer in PHP Certified Java Professional Certified Java EE Web Component Developer OMG Certified UML Professional ● MBA (cum laude) from Tel-Aviv University Information Systems Management lifemichael
  • 4.
    © 2008 HaimMichael 20151026 Oracle JDK is not Free  As of Java 11, the Oracle JDK would no longer be free for commercial use.  The Open JDK continues to be free. We can use it instead. However, we won't get nor security updates and nor any update at all.
  • 5.
    © 2008 HaimMichael 20151026 Running Java File  We can avoid the compilation phase. We can compile and execute in one command. We use the java command. It will implicitly compile without saving the .class file.
  • 6.
    © 2008 HaimMichael 20151026 Running Java File
  • 7.
    © 2008 HaimMichael 20151026 Java String Methods  The isBlank() method checks whether the string is an empty string, meaning... whether the string solely includes zero or more blanks. String with only white characters is treated as a blank string. public class Program { public static void main(String args[]) { var a = ""; var b = " "; System.out.println(a.isBlank()); System.out.println(b.isBlank()); } }
  • 8.
    © 2008 HaimMichael 20151026 Java String Methods  The lines() method returns a reference for a stream of strings that are substrings we received after splitting by lines. public class Program { public static void main(String args[]) { var a = "wenlovenjavanandnkotlin"; var stream = a.lines(); stream.forEach(str->System.out.println(str)); } }
  • 9.
    © 2008 HaimMichael 20151026 Java String Methods  The strip(), stripLeading()and the stripTrailing methods remove white spaces from the beginning, the ending and the remr of the string. It is a 'Unicode-Aware' evolution of trim(); public class Program { public static void main(String args[]) { var a = " we love kotlin "; var b = a.strip(); System.out.println("###"+b+"###"); } }
  • 10.
    © 2008 HaimMichael 20151026 Java String Methods  The repeat() method repeats the string on which it is invoked the number of times it receives. public class Program { public static void main(String args[]) { var a = "love "; var b = a.repeat(2); System.out.println(b); } }
  • 11.
    © 2008 HaimMichael 20151026 Using var in Lambda Expressions  As of Java 11 we can use the var keyword within lambda expressions. interface Calculation { public int calc(int a,int b); } public class Program { public static void main(String args[]) { Calculation f = (var a, var b)-> a+b; System.out.println(f.calc(4,5)); } }
  • 12.
    © 2008 HaimMichael 20151026 Using var in Lambda Expressions  When using var in a lambda expression we must use it on all parameters and we cannot mix it with using specific types.
  • 13.
    © 2008 HaimMichael 20151026 Inner Classes Access Control  Code written in methods defined inside inner class can access members of the outer class, even if these members are private. class Outer { private void a() {} class Inner { void b() { a(); } } }
  • 14.
    © 2008 HaimMichael 20151026 Inner Classes Access Control  As of Java 11, there are new methods in Class class that assist us with getting information about the created nest. These methods include the following: getNestHost(), getNestMembers() and isNestemateOf().
  • 15.
    © 2008 HaimMichael 20151026 Epsilon  As of Java 11, the JVM has an experimental feature that allows us to run the JVM without any actual memory reclamation.  The goal is to provide a completely passive garbage collector implementation with a bounded allocation limit and the lowest latency overhead possible. https://openjdk.java.net/jeps/318
  • 16.
    © 2008 HaimMichael 20151026 Deprecated Modules Removal  As of Java 11, Java EE and CORBA modules that were already marked as deprecated in Java 9 are now completely removed. java.xml.ws java.xml.bind java.activation java.xml.ws.annotation java.corba java.transaction java.se.ee jdk.xml.ws jdk.xml.bind
  • 17.
    © 2008 HaimMichael 20151026 Flight Recorder  The Flight Recorder is a profiling tool with a negligible overhead below 1%. This extra ordinary overhead allows us to use it even in production.  The Flight Recorder, also known as JFR, used to be a commercial add-on in Oracle JDK. It was recently open sourced.
  • 18.
    © 2008 HaimMichael 20151026 Flight Recorder  Using the jps command we can get the process id of our Java program.
  • 19.
    © 2008 HaimMichael 20151026 Flight Recorder  Using the jcmd command we can perform various commands, such as jcmd 43659 JFR.start
  • 20.
    © 2008 HaimMichael 20151026 Flight Recorder  Using the jcmd command together with JFR.dump we can dump all data to a textual file we choose its name and it location on our computer.
  • 21.
    © 2008 HaimMichael 20151026 Flight Recorder  There are various possibilities to process the new created data file. https://github.com/lhotari/jfr-report-tool
  • 22.
    © 2008 HaimMichael 20151026 The HTTP Client  As of Java 11, the HTTP Client API is more standardized.  The new API supports both HTTP/1.1 and HTTP/2.  The new API also supports HTML5 WebSockets.
  • 23.
    © 2008 HaimMichael 20151026 The HTTP Client  As of Java 11, the HTTP Client API is more standardized. The new API supports both HTTP/1.1 and HTTP/2. The new API also supports HTML5 WebSockets.
  • 24.
    © 2008 HaimMichael 20151026 The HTTP Client package com.lifemichael.samples; import java.io.IOException; import java.net.http.*; import java.net.*; import java.net.http.HttpResponse.*; public class HTTPClientDemo { public static void main(String args[]) { HttpClient httpClient = HttpClient.newBuilder().build();
  • 25.
    © 2008 HaimMichael 20151026 The HTTP Client HttpRequest request = HttpRequest.newBuilder().uri(URI.create( "http://www.abelski.com/courses/dom/lib_doc.xml")) .GET() .build(); try { HttpResponse<String> response = httpClient.send( request, BodyHandlers.ofString()); System.out.println(response.body()); System.out.println(response.statusCode()); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
  • 26.
    © 2008 HaimMichael 20151026 The HTTP Client  The new HTTP Client allows us to initiate an asynchronous HTTP request. HttpClient .sendAsync(request,BodyHandlers.ofString()) .thenAccept(response -> { System.out.println(response.body()); //.. });
  • 27.
    © 2008 HaimMichael 20151026 The HTTP Client  The new HTTP Client supports the WebSocket protocol. HttpClient httpClient = HttpClient.newBuilder() .executor(executor).build(); Builder webSocketBuilder = httpClient.newWebSocketBuilder(); WebSocket webSocket = webSocketBuilder .buildAsync( URI.create("wss://echo.websocket.org"), … ).join();
  • 28.
    © 2008 HaimMichael 20151026 Nashorn is Deprecated  As of Java 11, the Nashorn JavaScript engine and APIs are deprecated. Most likely, in future versions of the JDK.
  • 29.
    © 2008 HaimMichael 20151026 The ZGC Scalable Low Latency GC  As of Java 11, we can use the ZGC. This new GC is available as an experimental feature.  ZGC is a scalable low latency garbage collector. It performs the expensive work concurrently without stopping the execution of application threads for more than 10ms.
  • 30.
    © 2008 HaimMichael 20151026 The ZGC Scalable Low Latency GC  ZGC is suitable especially for applications that require low latency and/or use a very large heap (multi- terabytes).
  • 31.
    © 2008 HaimMichael 20151026 Files Reading and Writing  Java 11 introduces two new methods that significantly assist with reading and writing strings from and to files. readString() writeString()
  • 32.
    © 2008 HaimMichael 20151026 Files Reading and Writing public class FilesReadingDemo { public static void main(String args[]) { try { Path path = FileSystems.getDefault() .getPath(".", "welove.txt"); String str = Files.readString(path); System.out.println(str); } catch (IOException e) { e.printStackTrace(); } }
  • 33.
    © 2008 HaimMichael 20151026 Files Reading and Writing public class FilesWritingDemo { public static void main(String args[]) { Path path = FileSystems.getDefault() .getPath(".", "welove.txt"); try { Files.writeString(path, "we love php", StandardOpenOption.APPEND); } catch (IOException e) { e.printStackTrace(); } } }
  • 34.
    © 2008 HaimMichael 20151026 Q&A Thanks for attending our meetup! I hope you enjoyed! I will be more than happy to get feedback. Haim Michael haim.michael@lifemichael.com 0546655837