Home/Java/Values, references, and null

Values, references, and null

Primitive arithmetic, boxing, reference identity, and explicit absence

What a variable contains

Java primitives (int, long, double, boolean, char, and the smaller numeric types) hold values. Reference variables hold a reference to an object, or null; assigning a reference copies that reference, not the object. Method arguments are always passed by value, including the value of a reference. Reassigning a parameter cannot rebind the caller's variable, while mutating a referenced list changes the shared object.

java
var left = new ArrayList<>(List.of("a"));
var right = left;
right.add("b");
assert left.size() == 2;
right = new ArrayList<>();
assert left.size() == 2;

== compares primitive values and reference identity. Use .equals for logical object equality; Objects.equals handles nullable references. Autoboxing creates wrapper references such as Long, and unboxing a null wrapper throws NullPointerException. Wrapper caching makes identity checks appear to work for some numbers and fail for others, so never use it as numeric equality.

Arithmetic is a contract

An int expression is evaluated as int before assignment to long. Widen an operand before multiplying; widening a wrapped result cannot recover it. Integer division truncates toward zero. Overflow normally wraps silently; use Math.addExact/multiplyExact when overflow is an error. Floating-point arithmetic approximates real numbers; decimal money generally needs integer units or BigDecimal with an explicit rounding policy.

java
int width = 70_000;
long area = (long) width * width;
assert area == 4_900_000_000L;

Null is absence only when the API says so. Empty text, zero, and absence are different states. Validate at the boundary and keep the distinction in the return type; a Long can carry null while a long cannot. Local variables require definite assignment, but fields/array cells receive default zero/null values. final prevents reassignment; it does not freeze a referenced object.

Put it into practice

Implement the exercise in a full-screen workspace, then run its tests.

Open practice
Next lessonControl flow and arrays