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 }
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 }
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 }
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 }
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 } }
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!