Lesson 4 / 38

Variables & Types

Understand Java's primitive and reference types, declaring variables, final, local type inference with var, and wrapper classes with autoboxing.

Two kinds of types

Primitives hold a value directly: byte short int long float double char boolean. Reference types (classes, arrays, interfaces) hold a reference to an object on the heap; their default value is null.

Declaring

Types are fixed at declaration. var (Java 10+) infers the type of a local variable from its initializer — it is still statically typed, not dynamic.

int count = 10;
double price = 19.99;
boolean active = true;
String name = "Ada";
var total = count * price;   // inferred as double
final int MAX = 100;        // cannot be reassigned

Wrapper classes

Each primitive has an object wrapper (intInteger, doubleDouble). Autoboxing converts between them automatically, but boxed values can be null and comparing them with == compares references — use .equals() or .intValue().