Home > Java > javaTutorial > Local variable type inference in Java 10: How to use final var keyword in switch statement

Local variable type inference in Java 10: How to use final var keyword in switch statement

WBOY
Release: 2023-07-31 12:31:57
Original
906 people have browsed it

Local variable type inference in Java 10: How to use final var keyword in switch statement

As the Java language continues to evolve, each new version introduces some new features and improvements. In Java 10, one of the important new features is local variable type inference. This feature allows developers to use the var keyword instead of explicit type declarations, making the code more streamlined and readable. This article will explore how to use the final var keyword in a switch statement.

In past versions of Java, we had to explicitly declare the type for each local variable. For example, if we want to create a judgment condition in the switch statement, we must write like this:

int number = 1;
String message = "";
switch (number) {
    case 1:
        message = "One";
        break;
    case 2:
        message = "Two";
        break;
    case 3:
        message = "Three";
        break;
    default:
        message = "Unknown";
        break;
}
System.out.println("The number is: " + message);
Copy after login

In the above code, we need to declare an integer variable number and a string variable message. Then, we use the switch statement to check the value of number and assign it to the message variable according to different situations.

In Java 10, we can use the feature of local variable type inference to further simplify the code. We can use the var keyword instead of an explicit type declaration, and then let the compiler infer the type based on the variable's initialization value. Let's look at the following example:

final var number = 1;
final var message = switch (number) {
    case 1:
        yield "One";
    case 2:
        yield "Two";
    case 3:
        yield "Three";
    default:
        yield "Unknown";
};
System.out.println("The number is: " + message);
Copy after login

In the above code, we use the final var keyword to declare the number and message variables. Then, we use a switch expression to replace the switch statement. In Java 10, we can use yield keyword to return a value instead of using break statement in each case branch. In this way, we can merge logic and assignment operations together, making the code more concise and readable.

It should be noted that we use the final keyword to explicitly specify that the variable is immutable. This is because the type inferred by the var keyword is determined based on the variable's initialization value. If we change the value of message in one branch of the switch expression, the compiler will not be able to infer the correct type, resulting in a compilation error.

To summarize, local variable type inference in Java 10 allows us to use the var keyword instead of explicit type declarations, making the code more concise and readable. We can use the final var keyword in the switch statement to combine the switch expression and merge the logic and assignment operations together. However, it is important to note that we must explicitly specify that the variable is immutable to prevent compilation errors.

I hope this article can help you better understand local variable type inference in Java 10 and how to use the final var keyword in switch statements. With these new features, you can write more concise and readable code and improve development efficiency.

The above is the detailed content of Local variable type inference in Java 10: How to use final var keyword in switch statement. 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