JAVA 17 FEATURES
LTS stands for long-term support.
JDK enhancement proposal commonly referred to as JEPS as provided below:-
We can create a Record without using much line of code.
public record Student(int id, String name, String address)
{
}
406: Pattern matching for Switch(Preview)
(Parenthesized pattern ) && Gaurded pattern
409: Sealed Classes
306: Restore Always-Strict Floating-Point Semantics
356: Enhanced pseudo-Random Number Generators
403: Strongly Encapsulated JDK Internals
412: Foreign Functions & memory API(Incubator)
414: Vector API(Second Incubator)
415: Context-Specific Deserialization Filters
382: New macOS Rendering pipelines
391: macOS/AArch64 Port
398: Deprecate the Applet API for Removal
407: Removal RMI Activation
410: Removal Experimental AOT and JIT Compiler
411: Deprecate the Security manager for Removal
Let's talk about:- Developer kinds of stuff
406: Pattern matching for Switch(Preview)
It also allows the historical null-hostility of the switch to be relaxed when desired.
public class example
{
public static void main(String[] args) {
Object value = 6768.23;
switch (value)
{
case String str -> System.out.println("String length: " + str.length());
case Integer integer -> System.out.println("Integer Value: " + integer);
case Double dbl && dbl > 0 -> System.out.println("Positive double: " + dbl);
case null -> System.out.println("Value is null.");
default -> System.out.println("Unknown Value");
}
}
}
There are two new patterns introduced as follows:
- Guarded pattern: Uses pattern && boolean expression for further refinementpackage patternMatching.gaurdedPattern;
public class Example
{
public static void main(String[] args) {
Object value = -3212;
switch (value)
{
case String ans && ans.length() > 5 -> System.out.println("Given value is String and it's length is long" + ans.length());
case String ans && ans.length() < 5 -> System.out.println("Given value is short string and it's value is" + ans);
case Integer inte && inte >= 0 -> System.out.println("Given value is positive number and it's value is " + inte.intValue());
case Integer inte && inte < 0 -> System.out.println("Given value is negative number and it's value is " + inte.intValue());
default -> System.out.println("Enter a String or num bervalue");
}
}
//Earlier before java 17:-
static String formatNonEmptyString(Object o) {
switch (o) {
case String s:
if (s.length() >= 1) {
return String.format("String value is " + s);
} else {
return "Empty string";
}
default:
return o.toString();
}
}
}
- Parenthesized pattern
There is another pattern called parenthesized pattern. A parenthesized pattern is of the form(p), wherepis a pattern. You may already be familiar with the usage of parentheses in Java. The pattern matching for switch feature also allows you to use parentheses in case labels.package patternMatching.parenthesizedPattern;
public class example
{
public static void main(String[] args) {
System.out.println(formatValidString("xx"));
System.out.println(formatValidString("!"));
System.out.println(formatValidString("@@"));
}
static String formatValidString(Object o)
{
return switch(o)
{
case String s && s.length() >= 2 && (s.contains("@") || s.contains("!")) ->
String.format("Valid String value is %s", s);
default -> "Invalid Value";
};
}
}
409: Sealed Classes
Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them.
Allows you to choose its sub-class or restrict the access, A child class cannot extend a sealed class unless it's permitted by it's parent class.
Syntax:
public abstract sealed class Animal permits Dog, Cat, Rabbit {...} //This means only Dog Cat and Rabbit class have ability to extend Animal(parent) class.package patternMatching.sealedClasses;
public abstract sealed class Animal permits Dog, Cat
{
public static void main(String[] args) {
}
}
final class Dog extends Animal {
}
final class Cat extends Animal {
}
final class Fish extends Animal
{
//'Fish' is not allowed in the sealed hierarchy
}
Let's talk about:- Specific developer kinds of stuff
2.1 Restore Always-Strict Floating-Point Semantics:
It makes floating-point operations consistently strict.
Summary
Make floating-point operations consistently strict, rather than have both strict floating-point semantics (strictfp) and subtly different default floating-point semantics. This will restore the original floating-point semantics to the language and VM, matching the semantics before the introduction of strict and default floating-point modes in Java SE 1.2.
Goals
Ease development of numerically-sensitive libraries, including
java.lang.Mathandjava.lang.StrictMath.Provide more regularity in a tricky aspect of the platform.
2.2 Enhanced pseudo-Random Number Generators:
It provides a new interface type and implementations for pseudorandom number generators to make it easier to use various PRNG algorithms and to better support stream-based operations.
PRNGs use algorithms to generate a sequence of numbers that appear random, but are actually deterministic. This can be helpful for a variety of purposes, such as simulating random events, shuffling cards in a game etc.
the java.util.Random class is a commonly used PRNG, but it has some limitations. It uses a linear congruential generator (LCG) algorithm, which is not considered very secure, and it does not offer comprehensive options for customization.
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
public class EnhanceExample
{
public static void main(String[] args) {
RandomGenerator randomGenerator = RandomGeneratorFactory.of("Xoroshiro128PlusPlus").create(99);
int counter = 0;
while(counter <= 5)
{
int output = randomGenerator.nextInt(6);
System.out.println(output);
counter++;
}
}
}
2.3 Strongly Encapsulated JDK Internals:
It strongly encapsulates all non-critical internal elements of the JDK
Summary
Strongly encapsulate all internal elements of the JDK by default, except for critical internal APIs such as sun.misc.Unsafe. Allow end users to choose the relaxed strong encapsulation that has been the default since JDK 9.
Goals
Continue to improve the security and maintainability of the JDK, which is one of the primary goals of Project Jigsaw.
Encourage developers to migrate from using internal elements to using standard APIs, so that both they and their users can upgrade without fuss to future Java releases.
2.4 Foreign Functions & memory API(Incubator):
It introduces an API by which java programs can interpret code and data outside of the java runtime.
2.5 Vector API(Second Incubator):
It introduces an API to express vector computations that reliably compile at runtime to optimal vector instructions.
2.6 Context-Specific Deserialization Filters:
It allows applications to configure context-specific and dynamic-selected deserialization filters.
3.1 New macOS Rendering pipelines:
It changed the java 2D macOS rendering pipeline for macOS to use Apple Metal API instead of deprecated Apple OpenGL API.
3.2 macOS/AArch64 Port
Category 4: Cleaning up kinds of stuff
4.1 Deprecate the Applet API for Removal:
Applet API will be removed as it was deprecated since JDK9 most browsers do not support it anymore.
4.2 Removal RMI Activation:
Although RMI is still used, the RMI activation mechanism is obsolete with the web technology of the last decade.
4.3 Removal Experimental AOT and JIT Compiler:
Remove the experimental java-based ahead-of-time(AOT) and just-in-time(JIT) compiler.
4.4 Deprecate the Security manager for Removal:
Deprecate the Security Manager for removal in a future release. The Security Manager dates from java 1.0. It has not been the primary means of securing client-side java code for so many years.
Comments
Post a Comment