Home Java javaTutorial Local variable type inference in Java 10: How to simplify your code using var keyword

Local variable type inference in Java 10: How to simplify your code using var keyword

Jul 29, 2023 pm 07:32 PM
local variables type inference 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

  1. Declaration syntax
    The syntax for using the var keyword to declare a local variable is as follows:
    var variable name = expression ;
  2. 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.
  3. Inference rules
    The compiler infers the type of the variable based on the type of the assignment expression. The inference rules are as follows:
  4. 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.
  5. 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.
  6. If the assignment expression is a lambda expression, the compiler will infer the type of the variable to the corresponding function interface type.
  7. 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.

  1. 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 login

    The above code is equivalent to:

    int num = 10;
    System.out.println(num);
    Copy after login
  2. 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 login

    The above code is equivalent to:

    String name = "Java";
    System.out.println(name);
    Copy after login
  3. Collection 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 login

    The above code is equivalent to:

    ArrayList<String> list = new ArrayList<>();
    list.add("Java");
    System.out.println(list.get(0));
    Copy after login
  4. 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 login

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between local variables and global variables of a C++ function? What is the difference between local variables and global variables of a C++ function? Apr 19, 2024 pm 03:42 PM

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 Java 10: How to use var keyword in try-with-resources statement Local variable type inference in Java 10: How to use var keyword in try-with-resources statement Jul 30, 2023 pm 01:34 PM

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++ syntax error: When a function returns a pointer or reference, it cannot return a local variable or temporary object. What should I do? C++ syntax error: When a function returns a pointer or reference, it cannot return a local variable or temporary object. What should I do? Aug 22, 2023 am 09:22 AM

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

How can the type of return value of a PHP function be determined? How can the type of return value of a PHP function be determined? Apr 15, 2024 pm 10:51 PM

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).

Data competition analysis of global variables and local variables of Golang functions Data competition analysis of global variables and local variables of Golang functions May 21, 2023 am 08:19 AM

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 Java 10: How to simplify your code using var keyword Local variable type inference in Java 10: How to simplify your code using var keyword Jul 29, 2023 pm 07:32 PM

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

Function signatures and type inference in Go language Function signatures and type inference in Go language Jun 02, 2023 am 08:12 AM

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 Error: Java11 Local-Variable Syntax for Lambda Parameters (var keyword) error, how to handle and avoid Java Error: Java11 Local-Variable Syntax for Lambda Parameters (var keyword) error, how to handle and avoid Jun 24, 2023 pm 06:33 PM

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

See all articles