Home Java javaTutorial was a Java

was a Java

Oct 24, 2024 am 06:29 AM

var en Java

var is a construction that was introduced in JDK 10, it is used to create variables in which the data type is not specified, but rather the compiler is left to infer the type variable data. This is something known as type inference.

var is not a keyword or keyword of Java, but rather it is a reserved name of the language, as a result of it being introduced later and There is a possibility that it has been used as the name of some variable, class, method, etc. in code prior to its introduction.

The type inference is the process in which instead of declaring a variable with its data type, the compiler is allowed to determine the data type of the variable according to the value that is assigned to it, this is something that can be seen by creating a list and omitting the data type within the diamond operator, as shown below:

// Omite el tipo de dato dentro del operador diamante
List<Integer> list = new ArrayList<>();

// Usa el tipo de dato dentro del operador diamante
List<Integer> list = new ArrayList<Integer>();
Copy after login
Copy after login

How do you use var in Java?

To declare a variable with var, do as follows:

var nombreDeLaVariable = valor;
Copy after login
Copy after login
var n = "John";
Copy after login
Copy after login

In this case the compiler infers that the variable n is of type String because it is assigned a value of type String, and having to declare the data type is omitted, that is, the following does not have to be done:

String n = "John";
Copy after login
Copy after login

If the value of the variable is changed to 10, the compiler now infers that variable n is of type int:

var n = 10;
Copy after login
Copy after login

Similarly, if the value of the variable is changed to 10.0, the compiler now infers that variable n is of type double:

var n = 10.0;
Copy after login
Copy after login

Even if the value of the variable is changed to an instance of Random, the compiler now infers that variable n is of type Random:

// Omite el tipo de dato dentro del operador diamante
List<Integer> list = new ArrayList<>();

// Usa el tipo de dato dentro del operador diamante
List<Integer> list = new ArrayList<Integer>();
Copy after login
Copy after login

Limitations of var

  • When declaring attributes within a class, you cannot use var to declare them, since the compiler cannot infer the data type of an attribute, so you have to declare the data type explicitly.
var nombreDeLaVariable = valor;
Copy after login
Copy after login
var n = "John";
Copy after login
Copy after login
  • var cannot be used to declare parameters of a method or function, since the compiler cannot infer the data type of a parameter, so the data type must be declared explicitly.
String n = "John";
Copy after login
Copy after login
  • Among other limitations is the fact that you cannot use var when declaring a variable with a value null, since at least in Java, null is not a valid data type, and type inference does not work in this case.
var n = 10;
Copy after login
Copy after login
  • We also cannot use var to only declare a variable without initializing it, it is necessary to assign a value to the variable when declaring it.
var n = 10.0;
Copy after login
Copy after login
  • When several variables of the same type are declared on a single line (compound declarations), var cannot be used, failing which a variable with the explicit data type must be used.
var n = new Random();
Copy after login
  • var cannot be used to declare lambdas, method references or similar, for example:
public class Person {
    private var name; // Error
}
Copy after login

In the rest of the situations, var can be used in the normal way, considering that it must be used locally in the code and not at the level of attributes of a class, parameters of a method, etc. For example:

public class Person {
    private String name;
}
Copy after login
public void sayHello(var name) { // Error
    System.out.println("Hello " + name);
}
Copy after login

Something to consider is that in some cases using var can reduce the readability of the code, since Java is a language where variables are assigned data types that are known in advance, and when using var you can lose that information, so var can be used in situations where the data type can be inferred clearly and readability in the code is not lost.

var y = null; // Error
Copy after login

It is important not to confuse type inference with Java being a strongly typed language. Using var does not make the variable a dynamic type variable, but rather the compiler infers the data type of the variable at compile time. So you cannot declare a variable of type int and then assign it a value of type String.

var x; // Error
x = 10;
Copy after login

The above is the detailed content of was a Java. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles