Sub topic 10
No runtime cost and no dynamic typing
This chapter assumes you know basic Java declarations, primitive and reference types, and how the Java compiler checks types.
var is a compile-time feature. The compiler infers a type when it reads the declaration, then checks the rest of the program as if that type had been written explicitly. The JVM does not receive a special “var variable” at runtime.
Type inference happens during compilation
Consider this program:
public class CompileTimeVar {
public static void main(String[] args) {
var message = "Ready";
var count = 3;
message = message.toUpperCase();
count = count + 1;
System.out.println(message + " " + count);
}
}The compiler infers these declarations:
String message = "Ready";
int count = 3;After inference, the compiler checks each assignment and method call using those types. message cannot later refer to an integer:
public class FixedType {
public static void main(String[] args) {
var value = "text";
// value = 42; // compile-time error
System.out.println(value.length());
}
}The type of value is String for the entire scope. The variable does not change its type when the program runs.
`var` does not make Java dynamically typed
In a dynamically typed language, a variable can usually refer to values of unrelated types at different times:
value = "text"
value = 42Java does not work that way. A local variable declared with var still has one fixed static type. Static type means the type known to the compiler and used to check operations.
The following example shows the difference between the inferred type and the runtime object:
public class StaticAndRuntimeTypes {
public static void main(String[] args) {
var builder = new StringBuilder("Java");
builder.append(" var");
System.out.println(builder);
Object sameObject = builder;
System.out.println(sameObject.getClass().getName());
}
}The inferred static type of builder is StringBuilder. Therefore, the compiler allows builder.append(...).
The variable sameObject has the explicit static type Object, even though it refers to the same StringBuilder object at runtime. Through sameObject, the compiler only allows operations available on Object without a cast.
The object itself is not changed by using var. var only changes how the local declaration is written.
What reaches the JVM
Java source code is compiled into JVM bytecode. The JVM works with values, references, method calls, and local variable slots. It does not execute Java's var syntax.
This means var has no runtime dispatch mechanism, no runtime type inference step, and no general performance penalty. The compiler still emits the operations required by the inferred type.
A debugger may display local-variable information, and compiler-generated metadata can describe the local variable. That metadata does not mean the JVM has dynamic typing. It only helps tools inspect compiled code.
Compile-time errors remain compile-time errors
Because inference occurs before execution, invalid uses fail when the program is compiled:
public class CompileError {
public static void main(String[] args) {
var number = 10;
// number.toUpperCase(); // String method, but number is int
// number = "ten"; // String is not compatible with int
System.out.println(number);
}
}The program never reaches the main method if an uncommented invalid line causes compilation to fail.
Use var when the initializer makes the type clear and the inferred type gives the operations you need. Use an explicit type when readers need to see an abstraction, such as List<String>, or when the initializer hides an important design choice.
Exercise
Does var item = new ArrayList<String>(); make item dynamically typed, or can its type change later?
Show answer
It is statically typed as ArrayList<String>. The type is inferred during compilation, and item cannot later be assigned an unrelated type.