Learn Java
import com.learnjava.variablesdatatypes.Variables;

Variables in Java

@Concept

A variable is a named piece of memory that holds a value of one specific, declared type. Declaring a variable reserves storage shaped for that type; assigning to it stores an actual value there. Unlike Python, you can't just assign into a name and have it spring into existence — Java requires a type to be stated up front (or inferred with var), and that type never changes for the life of the variable.

Declaration and initialization can happen together or separately: int age; followed later by age = 25; does the same thing as int age = 25; written on one line. Since Java 10, the var keyword lets the compiler infer a local variable's type from whatever is assigned to it — var age = 25; still makes age a genuine, fixed int underneath. var is a shortcut for writing the type, not a step toward dynamic typing.

@HowItWorks
1

Declare

int age; reserves storage sized for an int and names it age, with no value stored yet.

2

Initialize

age = 25; stores an actual value into that reserved space. Declaring and initializing can also be combined into one line: int age = 25;

3

The type is fixed at declaration

Once age is declared as an int, it can only ever hold int values for the rest of its life.

4

Reassignment

age = 30; overwrites the previous value in the same memory location — only the contents change, not the variable's identity or type.

5

Type inference with var

var age = 25; lets the compiler infer int from the right-hand side, but age ends up exactly as statically typed as if int had been written by hand. var needs an initializer to infer from, and can never later hold a different type.

@Example
public class VariableDemo {
public static void main(String[] args) {
int age;
age = 25;
var name = "Ada";
System.out.println(name + " is " + age);
age = 26;
System.out.println(name + " is now " + age);
}
}
Output
Ada is 25
Ada is now 26
1int age; declares the variable without a value — it can't be read yet, only written to.
2age = 25; is a separate initialization step, storing 25 into that reserved space.
3var name = "Ada"; lets the compiler infer String from the literal on the right; name ends up exactly as strongly typed as String name = "Ada"; would be.
4Reassigning age = 26; changes the value in place, so the second println shows the updated number while name stays untouched.
@UseCases
Storing user input, computed results, or configuration values in essentially every method body
Understanding why var total; with no initializer fails to compile, since there's nothing to infer a type from
Reading pre-Java-10 code, which has no var at all
Weighing var against an explicit type for readability during code review

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.