Member-only story
Java var
For when you’re too lazy to write
Stringbut still want to pretend you’re working hard!
In Java, the var (Local-Variable Type Inference) is like having Java figure out the type of a variable for you, so you don’t have to write it yourself. It’s a shortcut for declaring variables without explicitly specifying their type.
Imagine going to a pizza place, and instead of telling the cashier what type of pizza you want (like cheese, pepperoni, etc.), you just say, “Give me whatever fits best,” and the cashier figures out the pizza type based on what you meant. That’s var!
How It Works:
- Before
var: You had to be specific about what type of value a variable would hold, like this:
String message = "Hello!";
int number = 10;- With
var: Java will guess the type of the variable based on the value you assign to it:
var message = "Hello!"; // Java knows it's a String
var number = 10; // Java knows it's an intThe key thing is, Java is still strongly typed. It knows exactly what type the variable is; you just don’t need to explicitly write it. It’s not dynamic typing like in JavaScript — it just saves you from typing long type names.











