Java Materials
← Core Java · Chapter 1

Platform and language fundamentals

Explain the separation between JDK tools, the runtime environment and the JVM. The JDK provides javac, java and development tools. javac emits class files, while the JVM uses class loaders, verifies bytecode and executes it by interpretation and JIT compilation. Bytecode can run on different operating systems when the JVM supports its class-file version and required APIs. Native libraries and filesystem behavior can still vary.

Bytecode, linking and initialization

Definition:

Bytecode is the JVM instruction format emitted by javac.

Explanation:

javac resolves language-level types and writes class files.

Illustration image:

Bytecode, linking and initialization. Example.java · Example.class · invokespecial · Loaded class metadata · Executable main()Bytecode, linking and initializationSource or actorExample.javaFirst boundaryExample.class · invokespecialInspected stateLoaded class metadataObservable effectExecutable main()
Example.java · Example.class · invokespecial · Loaded class metadata · Executable main()

Example:

Shell / diagnostic commands
javac Example.java
javap -c -v Example
java -Xlog:class+load=info,class+init=info Example

Expected behavior. Compare when a type is loaded and when it initializes; the logging flags are for modern HotSpot, available on Java 17 and 21.

Internal working:

Class loading finds bytes and defines a class; linking verifies them, prepares static storage and resolves symbolic references when required. Initialization executes static initializers. Reading a compile-time constant may leave the declaring class uninitialized because callers can inline it. A binary-compatible-looking constant change can therefore require recompiling callers.

Interview questions:

Why can reading a static final constant skip initialization?

A compile-time constant can be copied into the caller’s bytecode; no active use of the declaring class is required.

Numeric promotion, boxing and overflow

Definition:

Numeric promotion converts arithmetic operands to the operation’s required type before evaluation.

Explanation:

Arithmetic on byte and short generally promotes to int.

Illustration image:

Numeric promotion, boxing and overflow. int a × int b · int result can wrap · long a × int b · long resultNumeric promotion, boxing and overflowSource or actorint a × int bFirst boundaryint result can wrapInspected statelong a × int bObservable effectlong result
int a × int b · int result can wrap · long a × int b · long result

Example:

Worked Java example · see surrounding context in notes
long bytes = Math.multiplyExact(1024L, 4_000_000L);
Integer absent = null;
int available = absent == null ? 0 : absent;

Expected behavior. Multiplication produces 4,096,000,000 without overflowing int; the null wrapper is handled explicitly.

Internal working:

Casting the result to long after multiplying cannot recover overflow. Caching is an implementation/boxing guarantee for particular small constant values, not a general equality contract. For money, keep integer minor units with range checks or use BigDecimal plus a rounding policy. Null unboxing belongs in boundary validation.

Interview questions:

Why is (long)(a * b) too late for two int operands?

The multiplication already took place as int and may have overflowed; promote an operand before multiplication.

References, arrays and API boundaries

Definition:

A reference is a value identifying an object, and Java passes a copy of that value.

Explanation:

Java copies the value of every argument.

Illustration image:

References, arrays and API boundaries. Caller variable · Callee parameter copy · Shared array [changed] · New array [local only]References, arrays and API boundariesSource or actorCaller variableFirst boundaryCallee parameter copyInspected stateShared array [changed]Observable effectNew array [local only]
Caller variable · Callee parameter copy · Shared array [changed] · New array [local only]

Example:

Worked Java example · see surrounding context in notes
static void replaceFirst(String[] values) {
    values[0] = "changed";
    values = new String[]{"local only"};
}

Expected behavior. The caller observes changed in its array. The parameter reassignment stays local.

Internal working:

An array reference carries a runtime component type, enabling covariance but requiring a store check. Arrays.copyOf and clone copy reference slots rather than recursively cloning nested arrays. Define ownership with copies or an immutable view, and make module exports deliberate.

Interview questions:

Why can the callee mutate an array without replacing the caller’s variable?

Both reference values initially identify the same array. Reassigning the callee’s local value does not update the caller’s local.

Reference subtopics

Types and initialization

Primitives hold values; references identify objects or can be null. Scope says where a name is usable; lifetime concerns how long its storage or referent exists. Fields and array elements receive defaults; locals must be definitely assigned before use. Literal suffixes like L and F change the literal type. Use explicit numeric types when a boundary has a units or range contract.

Conversions and overflow

Widening often needs no cast, but long to float may lose precision. Narrowing can discard high bits or a fractional part. Binary numeric promotion makes arithmetic on byte and short typically happen as int. Signed integer arithmetic wraps except operations such as division by zero. Promote before multiplying or use Math.multiplyExact; use BigDecimal constructed from text for exact decimal money.

Boxing and parameter passing

Unboxing a null Integer throws. Wrapper identity can vary with caching; compare values using equals unless identity is intended. Java passes a copy of each argument value. Reassigning a reference parameter changes only that local copy; mutating its shared object can escape to the caller.

Control flow and arrays

&& and two vertical bars short-circuit; bitwise forms evaluate both sides. Use conditions, loops and explicit escape paths. Java 14 finalized switch expressions, available on 17 and 21. Arrays are fixed-size and covariant; storing a wrong runtime component type can throw ArrayStoreException. Multidimensional arrays are arrays of arrays and can be ragged. clone and Arrays.copyOf copy reference elements shallowly.

Packages and access

Imports shorten names; they do not import subpackages or override access. private, package access, protected and public establish different boundaries. JPMS adds module readability and exports or opens on top of Java access, so a public class can still be inaccessible outside its module.

Run the complete Java labs ↗ · Java reference ↗