Java var: Let Java Infer the Type
var flight = "BA-131" is a String, var bags = 2 an int, var tags = new ArrayList<String>() an ArrayList<String>. var total; and var gate = null won't compile, kg = "heavy" is still a type error, and var works in for-each loops and record patterns.
What you will learn
- Declare local variables with var and say which type Java picks
- Explain why var needs a value that has a type
- Show that the type is fixed once it's chosen
- Use var in a for-each loop and in a record pattern
Before you start
- Java variables, keywords and naming
- Java record patterns
Normally you write a variable's type yourself: String, int and so on. With var, Java reads the value on the right and works out the type on its own.
The type comes from the value
package org.studyeasy.s05l05;
import java.util.ArrayList;
public class Ex01_VarBasics {
public static void main(String[] args) {
// Explicit: you write the type on the left
String owner = "Hana";
int kg = 21;
// var: Java reads the type from the value on the right
var flight = "BA-131"; // String
var bags = 2; // int
var tags = new ArrayList<String>(); // ArrayList<String>
tags.add("BLR-1042");
tags.add("DEL-7710");
System.out.println(owner + ", " + kg + " kg, " + flight);
System.out.println(bags + " bags: " + tags);
}
}
Hana, 21 kg, BA-131
2 bags: [BLR-1042, DEL-7710]
Text in quotes is a String, a whole number is an int, and for new the type comes straight from what you create. Because tags is an ArrayList of String, add takes a String. Explicit and var variables mix freely.
No value, no type
var total; // cannot infer type for local variable total
var gate = null; // cannot infer type: the initializer is null
var total = 0; // fine: an int
With nothing on the right, there's nothing to read, so the compiler says cannot infer type for local variable total (var needs an initializer). null has no type of its own, so it fails the same way. Give it a value like 0 and total becomes an int. var always needs a value, and that value must have a type.
Chosen once, then fixed
var kg = 21; // Java picks int from the 21
System.out.println("Checked in: " + kg + " kg");
kg = 23; // another int: fine
System.out.println("After weighing: " + kg + " kg");
var owner = "Hana"; // a String from now on
System.out.println(owner.toUpperCase());
Checked in: 21 kg
After weighing: 23 kg
HANA
Java decides the type once, on the first line. 23 is another int, so only the value changes. kg = "heavy"; is a String going into an int variable: incompatible types: String cannot be converted to int. owner is a String, so toUpperCase works. After that first line, it's exactly as if you had written the type yourself. var isn't dynamic typing.
var in a for-each loop
var tags = List.of("BLR-1042", "DEL-7710", "LHR-0031");
// tag is a String: Java takes it from List<String>
for (var tag : tags) {
System.out.println(tag.toLowerCase());
}
var count = tags.size(); // an int, and it stays an int
System.out.println(count + " tags");
blr-1042
del-7710
lhr-0031
3 tags
The list is a List<String>, so Java stamps String onto tag on every pass, and toLowerCase works. size() returns an int, so count is an int. Inside the loop, int first = tag; fails, because tag is a String.
var in a record pattern
record Flight(String code, String to) {}
record Bag(String owner, Flight flight, int kg) {}
Object scan = new Bag("Hana", new Flight("BA-131", "London"), 21);
// Explicit types: every type written by hand
if (scan instanceof Bag(String owner, Flight flight, int kg)) {
System.out.println(owner + ": " + kg + " kg");
}
// var: the record already says the types
if (scan instanceof Bag(var owner, var flight, var kg)) {
System.out.println(owner + ": " + kg + " kg");
}
Hana: 21 kg
Hana: 21 kg
In a record pattern, there's nothing to guess. The record header already names every type, so var copies them: owner is a String, flight a Flight, kg an int. You still need one slot per component; Bag(var owner, var kg) is two of three and won't compile.
Recap
| Where | The type comes from |
|---|---|
var flight = "BA-131"; | the value on the right |
for (var tag : tags) | the list's element type |
Bag(var owner, var flight, var kg) | the record header |
var total; or var gate = null; | nowhere: compile error |
Once chosen, the type is fixed. Java reads the value; you keep the type.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.
Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.
Java Hello World with Maven: What the JDK Does and What Maven Adds
javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.
Java Variables, Keywords and Naming Rules
A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.
