‘var’ keyword in Java

Hashan Mahesh
4 min readMay 23, 2020

It seems like Java is creating a turning point, for a long time java developers are kind of restricted in a way of Object-Oriented thinking which is powerful at the production level. But that restriction forces us to do everything in that manner, no matter which kind of thing we are doing. For example, you have to type the following commands just for printing the hello world to the console.

public class Main{
public static void main(String[] args){
System.out.println("Hello World");
}
}

Don’t you feel like we are doing so many things just for printing a line, that’s because of the restriction in OOP. Here also we have to create a class, a main method and also inside that we are accessing the println() method in the System class. The same thing can be done in python using just a one-line.

print("Hello World")

But it seems like, Java 8 onwards, Java language designers are moving into designing and adding new features to Java which supports for more declarative and functional programming concepts. For example Lamdas and Streams.

LET’S GET MOVE ON TO THE TOPIC

I believe that the main point to introduce the “var” keyword in Java 10 is to provide the ability for Dynamic Typing which is a heavy concept used in many functional program languages including Javascript and Python.

Now before learning the use of ‘var’ keyword in Java, you should have a basic idea of the following topics.

  1. Type Safety
  2. Static Typing vs Dynamic Typing
  3. Type inference

FIRST THING FIRST ( Queue :)

What is Type Safety

Type safety is nothing but one of the abilities of a programming language which will help to categorize data types and assign specific set of operations/rules to them. For example Arithmetic operations for int data type are not applicable for strings. So this is simply called Type Safety.

Static Typing vs Dynamic Typing

Static Typed programming languages are those in which variables need not to be defined before they are used, which means you must initialize it to a variable name along with a data type before they are defined. Languages like Java, C are the examples for Static Typed Languages.

For example, if you want to create an integer variable you must have to initialize it first with a name and the data type as int/Integer in Java, but it is not necessarily need to be defined at the same time.

int a;a = 12;

Dynamic Typed programming languages are those in which variables must necessarily be defined before they are used. You can not initialize a variable without adding a value to it.

For example in Python, to create an integer variable, you have to do the following.

a = 12

To learn more about Static Typing and Dynamic Typing, I recommend you to visit here : — https://www.sitepoint.com/typing-versus-dynamic-typing/

What is Type Inference

Type inference is a compiler’s ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable. The inference algorithm determines the types of the arguments and, if available, the type that the result is being assigned, or returned.

For example:- in Java, to create an ArrayList of type String you could do the following.

ArrayList<String> arr = new ArrayList<String>();

The above statement can be written as follows by avoiding the extra “String” keyword which is defined at the right side of the statement.

ArrayList<String> arr = new ArrayList<>();

What happens here is that the Java compiler is smart enough to identify the ArrayList type as String even though you missed to type it at the right side, this identification is done through the left side of the statement. This special behavior in Java is called as Java Generic Type Inference.

What is the use of ‘var’ keyword in Java

the var keyword allows local variable type inference, which means the type for the local variable will be inferred by the compiler, so you don’t need to declare that.

var name = "Hashan";
var age = 23;
var ageToString = age.toString();
var profile = name + ageToString();

as you can see above, data types for all the variables are detected by the compiler.

What are the restriction for ‘var’

var keyword in Java follows Dynamic Typing concepts, which means you can not create a variable without defining it.

and also, you can not create class variables, method parameters and method return types as var’s.

The main point here is that the type of the variable is identified according to the data provided at the right side of that statement.

and also ‘var’ is not actually a keyword in Java, which means you can create a variable name as var.

int var = 12;
var var = 13;

--

--

Hashan Mahesh

I am an undergraduate at the University of Kelaniya, Sri Lanka. I study Information Communication Technology including Artificial Intelligence technologies