Java 23 programming language extends pattern matching and imports of classes

0
28
Java 23 programming language extends pattern matching and imports of classes


Advertisement


OpenJDK 23 was released every six months. Like its predecessor, the release brings twelve Java Enhancement Proposals (JEPs), most of which are in the preview stage.

Many JEPs continue proposals introduced in Java 21 and Java 22. The current JDK also contains two first previews. In addition to innovations in the language and libraries, there are also extensions to the documentation and improvements to the garbage collector. Finally, the JDK announces the end of unsafe memory access sun.misc.Unsafe One.

However, string templates are missing in Java 23 and will be reintroduced to the language later in a revised design.

Previous Java versions brought some innovations to pattern matching, enabling elegant management of data structures. “JEP 455: Primitive types in patterns, instances, and switches” As a new preview, it also enables the use of primitive data types within pattern matching for the first time. This means, among other things, case-Conditions specify not only numeric values ​​or reference types such as integers and strings, but also primitive types byte Or int Permission is granted, as in the following example from the proposal:

switch (x.getYearlyFlights()) {
  case 0 -> ...;
  case 1 -> ...;
  case 2 -> issueDiscount();
  case int i when i >= 100 -> issueGoldCard();
  case int i -> //... appropriate action when i > 2 && i < 100 ...
}

Type of variables in php switch-The statement does not have to match the data types in each case. This result corresponds to instanceof regardless of variable type trueProvided that the variable can be safely converted:

float f = 1000.0f;
f instanceof double; // true 
f instanceof int;    // true, da konvertierbar 
f instanceof byte;   // false, da jenseits des Raums von byte

The type doesn’t change, the result does trueIf lossless conversion is possible. The same applies to the record pattern.

Also marked as First Preview “JEP 476: Module Import Declarations (Preview)”which simplifies importing packages by including all packages that are exported by a module. So puts import java.base All the 54 packages defined in the module such as java.io, java.net And java.security prepare and thus save different imports using different commands import java.io.*,

Applications should explicitly resolve potential ambiguities. For example, the class exists Date both in java.utilit is in the module java.base is included, as well as java.sql. When importing both modules, the application that… Date Use, specify the import to use:

HashiCorp: New names and features for Terraform, Packer, and VaultHashiCorp: New names and features for Terraform, Packer, and Vault
import module java.base;
import module java.sql; 

import java.sql.Date;   // bestimmt die zu verwendende Date-Klasse

Date d = new Date();    // verwendet die Klasse aus java.sql

“JEP 477: Explicitly declared classes and instance main methods” First appeared in Java 21 as “JEP 445: Anonymous Classes and Instance Main Methods”. Most of all, it simplifies calling mainmethod and requires less boilerplate code. Among other things, the classic and previously not really minimal Hello World program has been significantly simplified:

//klassisches Hello World
public class HelloWorld { 
  public static void main(String() args) { 
    System.out.println("Hello, World!");
  }
}

//minimalistisches Hello World mit JEP 477
void main() {
  println("Hello, World!");
}

Aims for more flexibility in the constructor “JEP 482: Flexible Constructor Bodies”. In the second preview, it continues the proposal introduced in Java 22 as before “JEP 447: super(…)”. JEP breaks the old rule without any code super This is allowed when a Java constructor calls a constructor of a superclass.

Thanks to the innovation, for example, programs can check parameters to detect errors and avoid overhead by using the fail-fast principle before the superclass’s constructor is called:

public class PositiveBigInteger extends BigInteger {

  public PositiveBigInteger(long value) {
    if (value <= 0) throw new IllegalArgumentException(..);
    super(value);
  }
}

In addition, it is now possible to perform complex parameter calculations for the constructor of a superclass in a child class.

Also in Java 22 there was the first preview for processing classes and streams, which Java 23 continues. “JEP 466: Class File API” Java brings an API to parse, create and transform class files. As a standard for the core of Java, it should be able to process class files as well as third-party tools ASM or Apache Commons BCEL (Byte Code Engineering Library). It is designed as a simple API that is not intended to replace external libraries.




(Image: Aanchalie Thaveeboon/Shutterstock.com)

It will happen on October 15 BetterCode() Java 2024 instead. The online conference showcases the most important innovations from Java 21 to Java 23.

Theme day program Contains seven lectures on the following topics:

  • Java 23 new features at a glance
  • String templates under revision
  • Stream Gatherers for your own stream operations
  • Concurrent Programs in Java 23
  • Farewell to JNI for foreign function and memory APIs
  • More details thanks to the Class File API
  • Advanced features for advanced users

“JEP 473: Stream Gatherers” Java 22 flows into the current JDK without any changes compared to the first preview, and is for Java 24 Finalization of the proposal is planned under JEP 485. It extends the Java Stream API with additional operations that enable transformation of streamed data beyond that of previous extensions Stream::collect(). However, this collector can still stand behind the collector, and multiple collectors can be used in the form stream.gather(...).gather(...).collect(...) Mixing. The transformation through collectors can be in a 1:1, 1:n, n:1 or n:m relationship.

Product Activists: The AARRR Model — How Pirate Metrics Help Product OwnersProduct Activists: The AARRR Model — How Pirate Metrics Help Product Owners

LEAVE A REPLY

Please enter your comment!
Please enter your name here