Java 11 New Features
Java SE 11 was released in September 2018.
The full list of new features in JDK 11 can be found here:
http://openjdk.java.net/projects/jdk/11/
In this tutorial, we will look at some of these new features and examine some of them.
1. JEP 181: Nest-Based Access Control
Java 11 introduces the concept of nestmates. The main class and the types within that form a nest. Members of a nest are called nestmates of each other.
2. JEP 318: Epsilon: A No-Op Garbage Collector
Epsilon is a No-Op Garbage Collector which does collect nothing and when the heap is full then it will shut down the jvm. It could be used for testing use-cases.
Epsilon GC is experimental and could be used with the following jvm parameters:
-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC
3. JEP 320: Remove the Java EE and CORBA Modules
Some Java EE modules had been included in Java SE 6. This is reevaluated and thought that these are intentionally for Java EE and to be removed from Java SE, then already deprecetaed in Java 9. And with Java 11, these modules will not be part of Java SE.
java.xml.ws
(JAX-WS, plus the related technologies SAAJ and Web Services Metadata)java.xml.bind
(JAXB)java.activation
(JAF)java.xml.ws.annotation
(Common Annotations)java.corba
(CORBA)java.transaction
(JTA)
4. JEP 321: HTTP Client (Standard)
The Http Client API, which is introduced in Java 9, is now standardized in Java 11. The API supports both Http 1.1 and Http 2 and Websocket.
Here is an example:
HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofMillis(3000))
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create("https://www.google.com"))
.setHeader("User-Agent", "Java 11 HttpClient")
.build();
HttpResponse<String> response =
httpClient.send(request, HttpResponse.BodyHandlers.ofString());
HttpHeaders headers = response.headers();
headers.map().forEach((k, v) -> System.out.println(k + ": " + v));
5. JEP 323: Local-Variable Syntax for Lambda Parameters
Java 10 introduced var keyword for local variable type inference but that could not be used within lambda expressions. JEP 323 adds this feature.
An example here:
var list = List.of("a", "b", "c");
list.forEach((var s) -> System.out.println(s));
6. JEP 327: Unicode 10
Java 11 supports Unicode 10 and that means more characters to support including emojis.
public static void main(String[] args) {
String codepoint = "U+1F603"; // smiling face with open mouth
System.out.println(codePoints(codepoint));
}
// UTF-16
static char[] codePoints(String codePoint) {
Integer i = Integer.valueOf(codePoint.substring(2), 16);
return Character.toChars(i);
}
7. JEP 328: Flight Recorder
8. JEP 330: Launch Single-File Source-Code Programs
It's now possible to run a single-file java source code with directly java command, no need to explicitly compile the source code.
java HelloWorld.java
Of course this makes it easy for us to run simple java programs. Besides this, the JEP also makes it possible to run java programs from within linux shebang files. A shebang or hashbang is the character sequence #! at the beginning of a script file that tells the unix system how to interpret the file. So, that means you can write simple linux bash scripts with java.
Here is a sample my-sample-script.sh file:
#!/usr/bin/java --source 11
public class SheBangScript {
public static void main(String[] args) {
System.out.println("Hello World in Shebang!");
}
}
No need to compile, just run ./my-sample-script.sh and it works.
332: Transport Layer Security (TLS) 1.3
Java 11 supports Transport Layer Security (TLS) 1.3 protocol but with not all feature set.
333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
ZGC, is a scalable low-latency garbage collector experimentally included in Java 11 for Linux/64 systems.
Yorumlar
Yorum Gönder