Home >Java >javaTutorial >was a 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>();
To declare a variable with var, do as follows:
var nombreDeLaVariable = valor;
var n = "John";
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";
If the value of the variable is changed to 10, the compiler now infers that variable n is of type int:
var n = 10;
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;
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>();
var nombreDeLaVariable = valor;
var n = "John";
String n = "John";
var n = 10;
var n = 10.0;
var n = new Random();
public class Person { private var name; // Error }
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; }
public void sayHello(var name) { // Error System.out.println("Hello " + name); }
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
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;
The above is the detailed content of was a Java. For more information, please follow other related articles on the PHP Chinese website!