About var used in Java (Local Variable Type)

This article was written for people who were programming in other languages and would like to program in Java.

What is Local Variable Type?

Roughly speaking, it seems that var can now be used in Java as well.

What is var

There are vars in multiple languages, but here we will focus only on Java vars. var stands for "variable". As the name implies, it is used when using variables. However, in Java it can only be used for local variables. var is like a type var variable name = variable content; However, unlike other variables, the contents of the variable are always required. This is because the type of var changes depending on the contents of the variable.

Specific examples include the following.


class Main {
  public static void main(String[] args) {
    var i = 5; //int type
    var msg = "Hello World!"; //String type
    var c = 'Ah'; //char type
    System.out.println(msg+i); //Hello World!5 is displayed
    System.out.println(c); //A is displayed

For i, var is treated as an integer type int because the left side is an integer. Thus, var can only be used if you can clearly guess this language on the left side.

Significance of var

var is a type inference type, and a fixed type seems easier to understand, but it has a big advantage. That is, you can omit the type specification.

Type specification can be omitted

This is also difficult to understand if it is only sentences, so I will describe two examples.

  1. List, class description


List<String> Obj1 = new ArrayList<String>(); //No var
var Obj2 = new ArrayList<string>(); //with var


class Main {
  public static void main(String[] args) {
    longname_callclass cc = new longname_callclass(); //No var
    var vcc = new longname_callclass(); //with var

class longname_callclass {
  public void callmethod() {
    System.out.println("Successful call");

Since the left type can be omitted in this way, the sentence becomes shorter and the result becomes easier to read.

  1. Number description


var i = 1; // int
var l = 3L; // long
var d = 1.0; // double
var f = 4.2f; // float;

Integer types allow int and long to be separated with and without L, so you can see the type by looking only at the numbers.

Recommended Posts

About var used in Java (Local Variable Type)
About java var
[Java] Use of final in local variable declaration
Type determination in Java
[Java] About enum type
Variable type in ruby
About the meaning of type variables, E, T, etc. used in generics used in Java
[Java] Things to note about type inference extended in Java 10
Try functional type in Java! ①
About Java variable declaration statements
About abstract classes in java
Syntax examples often used in Java
About methods often used in devise
About file copy processing in Java
About returning a reference in a Java Getter
[Easy-to-understand explanation! ] Reference type type conversion in Java
[Java] Display the bit string stored in the byte type variable on the console
[Java ~ Variable definition, type conversion ~] Study memo
[Creating] A memorandum about coding in Java
About Records preview added in Java JDK 14
[Rails] About local: true described in form_with
[Basic knowledge of Java] About type conversion
Continued Talk about writing Java in Emacs @ 2018
About the confusion seen in startup Java servers
[Introduction to Java] About type conversion (cast, promotion)
About the idea of anonymous classes in Java
A story about the JDK in the Java 11 era
Try scraping about 30 lines in Java (CSV output)
The intersection type introduced in Java 10 is amazing (?)
[Android / Java] Operate a local database in Room
Organized memo in the head (Java --Data type)
About regular expressions used in ruby sub method
Java type conversion
About Java interface
[Java] About Java 12 features
java array variable
[JAVA] Stream type
Partization in Java
[Java] About arrays
[Java] Enumeration type
Java Optional type
Changes in Java 11
Rock-paper-scissors in Java
Something about java
Where about java
Java variable scope (scope)
About Java features
java variable declaration
About Java threads
[Java] About interface
Java double type
About Java class
Java variable scope
About Java arrays
About java inheritance
About interface, java interface
Pi in Java
Java local class
FizzBuzz in Java
About List [Java]
About Java literals