


Local variable type inference in Java 10: How to simplify your code using var keyword
Local variable type inference in Java 10: How to use the var keyword to simplify code
Introduction:
In Java 10, the feature of local variable type inference is introduced. By using the var keyword, Can simplify the coding process. This article will introduce the use of the var keyword and demonstrate its effect of simplifying the code through sample code.
1. What is local variable type inference?
Local variable type inference means that when declaring local variables, you can use the var keyword instead of explicit type declaration. The compiler will infer the type of the variable based on the type of the assignment expression to simplify code writing.
2. How to use the var keyword
- Declaration syntax
The syntax for using the var keyword to declare a local variable is as follows:
var variable name = expression ; - Variable initialization
When using the var keyword to declare a local variable, it must be initialized. The compiler infers the type of a variable from the type of the assignment expression. - Inference rules
The compiler infers the type of the variable based on the type of the assignment expression. The inference rules are as follows: - If the type of the assignment expression is clear (such as int, String, etc.) , the compiler will infer the variable's type to that type.
- If the assignment expression is a generic method call, the compiler will infer the type of the variable to the type of the actual type parameter.
- If the assignment expression is a lambda expression, the compiler will infer the type of the variable to the corresponding function interface type.
- If the type of the assignment expression is a constructor of a collection or array of unknown type, the compiler will infer the type of the variable to the type of the collection or array.
3. Examples of using the var keyword to simplify the code
The following is a few example codes to demonstrate the effect of using the var keyword to simplify the code.
-
Basic type variable declaration
The code using the var keyword to declare a basic type variable is as follows:var num = 10; System.out.println(num);
Copy after loginThe above code is equivalent to:
int num = 10; System.out.println(num);
Copy after login Reference type variable declaration
The code using the var keyword to declare a reference type variable is as follows:var name = "Java"; System.out.println(name);
Copy after loginThe above code is equivalent to:
String name = "Java"; System.out.println(name);
Copy after loginCollection type variable declaration
The code using the var keyword to declare a collection type variable is as follows:var list = new ArrayList<String>(); list.add("Java"); System.out.println(list.get(0));
Copy after loginThe above code is equivalent to:
ArrayList<String> list = new ArrayList<>(); list.add("Java"); System.out.println(list.get(0));
Copy after login-
lambda expression variable declaration
The code to declare a lambda expression variable using the var keyword is as follows:var runnable = (Runnable) () -> { System.out.println("Hello Java 10!"); }; runnable.run();
Copy after loginThe above code is equivalent to:
Runnable runnable = (Runnable) () -> { System.out.println("Hello Java 10!"); }; runnable.run();
Copy after login
Summary:
By using the var keyword, you can simplify the code writing process, especially during the declaration of local variables. However, in actual use, the var keyword needs to be used with caution to avoid reducing the readability of the code. In the case of strong readability, you can use the var keyword to simplify the code and improve development efficiency.
The above is the detailed content of Local variable type inference in Java 10: How to simplify your code using var keyword. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The difference between C++ local variables and global variables: Visibility: Local variables are limited to the defining function, while global variables are visible throughout the program. Memory allocation: local variables are allocated on the stack, while global variables are allocated in the global data area. Scope: Local variables are within a function, while global variables are throughout the program. Initialization: Local variables are initialized when a function is called, while global variables are initialized when the program starts. Recreation: Local variables are recreated on every function call, while global variables are created only when the program starts.

Local variable type inference in Java10: How to use var keyword in try-with-resources statement Introduction: Java10 has made some improvements in local variable type inference. The introduction of the var keyword allows developers to omit the type when declaring variables, and the compiler will infer it. This article will focus on how to use the var keyword in the try-with-resources statement. 1. What is try-with-reso?

C++ is an object-oriented programming language, and its flexibility and power often provide programmers with great help. However, precisely because of its flexibility, it is difficult to avoid various small errors when programming. One of the most common mistakes is that when a function returns a pointer or reference, it cannot return a local variable or temporary object. So how to deal with this problem? This article will introduce the relevant content in detail. The cause of the problem is that in the C++ language, local variables and temporary objects are dynamically allocated during the running of the function. When the function ends, these local variables and temporary

Methods to determine the return value type of a PHP function include: 1. Using typehint declaration; 2. Inferring based on function definition; 3. Using gettype() function; 4. Using third-party libraries (such as Psalm and PHPStan).

Golang is a strongly typed programming language with features such as efficiency, simplicity, and concurrency, so it is gradually favored by more and more developers. In the development of Golang, the global variables and local variables of functions often involve data competition issues. This article will analyze the data competition problem of global variables and local variables in Golang functions from the perspective of actual coding. 1. Data competition for global variables Golang global variables can be accessed in all functions, so if rigorous design and coding are not carried out

Local variable type inference in Java10: How to use the var keyword to simplify code Introduction: In Java10, the feature of local variable type inference is introduced. By using the var keyword, the code writing process can be simplified. This article will introduce the use of the var keyword and demonstrate its effect of simplifying the code through sample code. 1. What is local variable type inference? Local variable type inference means that when declaring local variables, you can use the var keyword instead of explicit type declaration. The compiler will express

1. Function signature Function is an important way to encapsulate code in programming. Functions provide a way to encapsulate a block of code and reuse it when needed. In Go, functions are first-class values that can be passed to and returned from functions just like other types of values. A function signature describes the function's inputs and outputs, including the number of parameters, their types, and the return value type. The syntax of function signature in Go language is as follows: funcfunctionName(parameter1type1,

Java has introduced a new keyword var in the latest version Java11, which can replace type names that need to be explicitly declared, thereby simplifying code and improving readability. However, when using Lambda expressions, the var keyword sometimes causes a Local-VariableSyntaxforLambdaParameters error. This article will introduce the cause of this error, and how to deal with and avoid it. Why does this error occur? Before Java11, La
