[Java Siler] About type inference by var

What is var

A mechanism for performing ** type inference ** of ** local variables **, which has appeared since Java SE10. Infer the data type from the type on the right side. Type inference with var is done at compile time.

** * [What is a local variable] ** A variable declared in ** in a method ** (used as a formal argument or argument in a method)

** ・ Example of type inference by var **

var i = 10;  //Infer that i is an int type from the value on the right side

** Only local variables can be type inferred by var! !! !! !! ** **

Pattern that cannot use var (compile error occurs)

-For variables other than local variables (instance variables, static variables, ** method return type **) -If not initialized when the variable is declared (that is, if only "var i;" and the variable are created) ・ When initialized with null -For variables that assign array initialization

Precautions when creating an array with var

var infers the data type from the type on the right side. Then, the initializer ({}) used to create the array also infers the type of the array from the type of the value to be assigned. Since they infer each other, the type is not determined and a compile error occurs.

**-Example of array initialization that causes a compile error **

var array = {1,2,3};

However, if you declare the type when creating the value of the array on the right side, you can initialize the array with var.

** ・ Example of array initialization by var **

var array = new int []{1,2,3};  //You can create an int type array

Precautions when creating ArrayList with var

As with arrays, you can also create an ArrayList with var.

**-Initialize ArrayList with var **

var list = new ArrayList<>();

The ** diamond operator ** of ArrayList returns ** Object type ** when there is no referenceable type. Therefore, the above code is the same as the code below.

 var list = new ArrayList<Object>();

An Object type ArrayList is created as a list.

Recommended Posts

[Java Siler] About type inference by var
About java var
[Java] About enum type
About var used in Java (Local Variable Type)
[Java] Things to note about type inference extended in Java 10
[JDK 10] Type inference using var
[Basic knowledge of Java] About type conversion
Java type conversion
About Java interface
[Java] About Java 12 features
[Introduction to Java] About type conversion (cast, promotion)
[JAVA] Stream type
[Java] About arrays
[Java] Enumeration type
Java Optional type
Something about java
Where about java
About Java features
About Java threads
[Java] About interface
Java double type
About Java class
About Java arrays
About java inheritance
About interface, java interface
About List [Java]
About Java literals
About Java commands
About Java basic data types and reference type memory
About CLDR locale data enabled by default from Java 9
About Java log output
About Java functional interface
Java, about 2D arrays
About class division (Java)
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
[Java] About Singleton Class
[Java] Data type ①-Basic type
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
[Java Silver] About initialization
About Java Array List
About Java Polymorphism super ()
[Java, Kotlin] Type Variance
Java class type field
Type determination in Java
Java study # 1 (typical type)
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
About Apache Inference API
All about Java programming
About java abstract class
[Java] Date type conversion
Difference in included SDK (Jar) by Java8 type JDK Vender
[For beginners] About the JavaScript syntax explained by Java Gold