Home > Java > javaTutorial > body text

How to use Pattern Matching to determine and convert data types in Java 14

WBOY
Release: 2023-07-29 14:13:26
Original
1389 people have browsed it

How to use Pattern Matching to determine and convert data types in Java 14

In Java 14, Pattern Matching for instanceof (ie "pattern matching") was introduced, which can simplify the determination and conversion of data types. conversion operation. This new feature can make the code more concise and readable, while improving development efficiency. This article will introduce how to use Pattern Matching to determine and convert data types in Java 14, and provide code examples.

1. Pattern type judgment

Pattern Matching for instanceof allows the instanceof operator to be directly used in combination with variable declaration. The previous way of writing was to first perform type judgment and then perform type conversion, as shown below:

if(obj instanceof String) {
    String str = (String) obj;
    // do something with str
}
Copy after login
Copy after login

In Java 14, we can directly use pattern type judgment, and the code is as follows:

if(obj instanceof String str) {
    // do something with str
}
Copy after login
Copy after login

In this way, we can not only determine whether obj is of String type, but also coerce obj to String type and use the str variable directly in the if statement.

2. Pattern type conversion

In addition to type judgment, Pattern Matching also provides the function of pattern type conversion, which can easily convert objects to specified types. The previous way of writing was to use forced type conversion, as shown below:

if(obj instanceof String) {
    String str = (String) obj;
    // do something with str
}
Copy after login
Copy after login

In Java 14, we can use pattern type conversion to replace the above writing method, the code is as follows:

if(obj instanceof String str) {
    // do something with str
}
Copy after login
Copy after login

This The writing method can not only perform type judgment, but also convert obj to String type. We can use the str variable directly in the if statement.

3. Nested use of pattern type judgment and conversion

Pattern Matching also supports the nested use of pattern type judgment and pattern type conversion, which can perform multiple judgments and conversions more flexibly. The following is a sample code:

if(obj instanceof Shape shape) {
    if(shape instanceof Circle circle) {
        // do something with circle
    } else if(shape instanceof Rectangle rectangle) {
        // do something with rectangle
    } else {
        // do something else
    }
}
Copy after login

In the above code, first determine whether obj is of Shape type, and convert obj into an object shape of Shape type; then judge and convert again based on shape, judge Whether it is a Circle type or a Rectangle type, and operate using the circle and rectangle variables respectively.

Summary

Pattern Matching for instanceof is an important feature introduced in Java 14, which can simplify the judgment and conversion of data types, and improve code readability and development efficiency. This article introduces how to use Pattern Matching to determine and convert data types in Java 14, and provides corresponding code examples. In actual development, we can make full use of this new feature to make the code more concise, readable and efficient.

The above is the detailed content of How to use Pattern Matching to determine and convert data types in Java 14. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!