Memra

Variables, assignment & var

Declaring, initialising, and letting Java infer the type.

Naming values

A variable is a typed name bound to a value:

int count = 10;      // declare + initialise
count = count + 1;   // reassign
final int MAX = 100; // final = can't be reassigned

A local variable must be assigned before you read it — Java enforces "definite assignment" at compile time.

Since Java 10, var lets the compiler infer the type of a *local* variable from its initialiser:

var name = "Ada";    // inferred String
var total = 0;        // inferred int
var list = new ArrayList<String>();

var is still statically typedname is permanently a String, you just didn't write the word. It only works for local variables *with* an initialiser; you can't use it for fields, method parameters, or var x; with no value.

NORMAL ~/memra/learn/java-from-zero/variables-and-var utf-8 LF