Java 11 Features @pankaj
Java 11 Features @pankaj
Java 11 Features
——————————
1. Importance of Java 11.
2.Running Java File with single command.
3.Java String Method.
4.Local-Variable Syntax for Lambda Parameters.
5.Nested based Access Control.
6.Dynamic class-file Constants
7.Epsilon: A No-Op Garbage Collector
8.Remove the Java EE and CORBA Modules
9.Flight Recorder
10.HTTP Client API
11. Reading/Writing String to and from the Files.
12.ChaCha20 and Poly 1305 Cryptographic Algorithms
13.Improve Aarch64 Intrinsics.
14.ZGC: A Scalable Low-Latency Garbage Collector
15. Deprecate the Nashorn JavaScript Engine
Java 11 Features.
isBlank() – This instance method returns a boolean value. Empty Strings and
Strings with only white spaces are treated as blank.
import java.util.*;
String s = "Anupam";
System.out.println(s.isBlank()); //false
String s1 = "";
System.out.println(s1.isBlank()); //true
}
}
JD
JD
JD
[JD, JD, JD]
System.out.print("Start");
System.out.print(str.stripLeading());
System.out.println("End");
System.out.print("Start");
System.out.print(str.stripTrailing());
System.out.println("End");
}
}
The output in the console from the above code is:
StartJDEnd
StartJD End
Start JDEnd
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.
public class Main {
public static void main(String[] args) throws Exception {
// Your code here!
Local-Variable Syntax for Lambda Parameters is the only language feature release in Java 11
s1 -> s1.toUpperCase()
we cannot skip them while using var:
var s1 -> s1.toUpperCase()
class NestedMain{
void msg() {
display();
}
}
}
}
Output:-
hello from private method
Extend the Java class-file format to support a new constant-pool form, CONSTANT_Dynamic. Loading
a CONSTANT_Dynamic will delegate creation to a bootstrap method, just as linking an invoked
dynamic call site delegates linkage to a bootstrap method.
Future work
Possible future extensions include:
Support for bulk-scale constants such as arrays or resource tables
Further adjustments to the bootstrap-method handshake
Other uses of bootstrap methods which may synergise with dynamic
constants
Attaching dynamic constants to the ConstantValue attribute of static
fields
Surfacing the lazy initialisation of constants in the Java language
Integrating new constants with special Java language rules for constant
expressions
A discussion of design choices can be found in jdk 8 which deals with a
number of related RFEs. The present JEP was distilled from this larger list of
features.
The modules were already deprecated in Java 9. They are now completely
removed.
Following packages are removed:
Name Module Description
Java API for XML Web java.xml.ws Defines the Java API for XML-Based Web
Services (JAX-WS) Services (JAX-WS), and the Web Services
Metadata API.
jdk.xml.ws Tools for JAX-WS
Java Architecture for XML java.xml.bind Defines the Java Architecture for XML Binding
Binding (JAXB) (JAXB) API.
jdk.xml.bind Tools for JAXB
JavaBeans Activation Defines the JavaBeans Activation Framework
java.activation
Framework (JAF) (JAF) API.
Common Annotations java.xml.ws.ann Defines a subset of the Common Annotations
otation API to support programs running on the Java
SE Platform.
Common Object Request
Defines the Java binding of the OMG CORBA
Broker Architecture java.corba
APIs, and the RMI-IIOP API.
(CORBA)
Java Transaction API java.transaction Defines a subset of the Java Transaction API
(JTA) (JTA) to support CORBA interoperation.
Aggregator module for the java.se.ee Defines the full API of the Java SE Platform.
all modules above
9.Flight Recorder
HTTP Client
—————————
Http CLient API.
The new API supports both HTTP/1.1 and HTTP/2. It is designed to improve
the overall performance of sending requests by a client and receiving
responses from the server. It also natively supports WebSockets.
Example 1.
package jdk11;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class httpClient {
var request=HttpRequest.newBuilder().GET().uri(URI.create(url)).build();
var client=HttpClient.newBuilder().build();
System.out.println(response.statusCode());
System.out.println(response.body());
}}
Example 2.
package jdk11;
import java.io.IOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
var url="https://www.google.com";
var client=HttpClient.newBuilder()
//.authenticator(Authenticator.getDefault())
.connectTimeout(Duration.ofSeconds(200))
//.cookieHandler(null)
//.executor(null)
.followRedirects(HttpClient.Redirect.NORMAL)
// .proxy(ProxySelector.of(new InetSocketAddress("https://www.google.com",8080)))
//.sslContext(null)
//.sslParameters(null)
.build();
var request=HttpRequest.newBuilder()
.uri(URI.create(url))
.GET()
.version(HttpClient.Version.HTTP_2)
.build();
System.out.println(response.statusCode());
//System.out.println(response.body());
Example 3-> for reading json data from particular url path, we also can
perform CRUD operation by using of PUT,POST,PATCH,DELETE Method at
place of GET method.
Step-1.
package jdk11;
Step 2:-
package jdk11;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.*;
HttpResponse<String> response=client.send(request,
HttpResponse.BodyHandlers.ofString());
posts.forEach(System.out::println);
System.out.println(response.statusCode());
}
}
output:- url has no json data so throwing exception
How it works?
The inputs to ChaCha20 encryption and decryption:
A 256-bit secret key (32 bytes)
A 96-bit nonce (12 bytes)
A 32-bit initial count (4 bytes)
ChaCha20 Encryption.
(plain text) + (secrect key | nonce | initial count) -> `ChaCha20` -> ciphertext
(encrypted text).
ChaCha20 Decryption.
ciphertext + (secrect key | nonce | initial count) -> `ChaCha20` -> plain text.
The ChaCha20 encryption uses the key and IV (initialization value, nonce +
initial count) to encrypt the plaintext into a ciphertext of equal length.
The nonce and secret key must be unique for each encryption. The nonce
and initial count are ok to be publicly known, but the secret key must be
private and keep it confidential.
ChaCha20 Java Implementation
Download the JDK source code and find this class ChaCha20Cipher for
ChaCha20 algorithm implementation.
ChaCha20Cipher.java
package com.sun.crypto.provider;
/**
* Implementation of the ChaCha20 cipher, as described in RFC 7539.
*
* @since 11
*/
abstract class ChaCha20Cipher extends CipherSpi {
//...
}
13.Improve Aarch64 Intrinsics.
Improve the existing string and array intrinsics, and implement new intrinsics
for the java.lang.Math sin, cos, and log functions, on AArch64 processors.
Goals
GC pause times should not exceed 10ms
Handle heaps ranging from relatively small (a few hundreds of
megabytes) to very large (many terabytes) in size
No more than 15% application throughput reduction compared
to using G1
Lay a foundation for future GC features and optimisations
leveraging coloured pointers and load barriers
Initially supported platform: Linux/x64
We have strong ambitions to meet these goals for a large set of
relevant workloads. At the same time, we want to acknowledge that
we don't see these goals as hard requirements for every
conceivable workload.
Nashorn JavaScript script engine and APIs are deprecated thereby indicating
that they will be removed in the subsequent releases.
This removal does not affect, in any way, the javax.script API.