Every Java value is either a primitive or a reference. There is no third kind in LTS Java (value types are not a 17–21 production feature). A variable’s declared type decides which operations compile; the runtime class of an object decides virtual dispatch.
| Primitives | References | |
|---|---|---|
| Values | boolean, byte, short, char, int, long, float, double | Classes, interfaces, arrays, null |
| Variable holds | The value itself | A pointer, or null |
| == | Value equality | Identity (same object), not content |
| Default field | 0 / false / '\0' | null |
| Copy on assign | The bits | The pointer (not a deep copy) |
| Type | Bits | Notes |
|---|---|---|
| boolean | — | Not an int. No numeric conversions. |
| byte | 8 | Signed. Arithmetic promotes to int. |
| short | 16 | Signed. Same promotion. |
| char | 16 | Unsigned UTF-16 code unit. 'A' + 1 is an int. |
| int | 32 | Default integer type. Wraps in two’s complement. |
| long | 64 | Write 1L. Integer literals are int unless suffixed or too big. |
| float | 32 | IEEE-754. Suffix f. |
| double | 64 | Default floating type. Not decimal. |
int x = Integer.MAX_VALUE + 1; // MIN_VALUE — no exception long y = 2_000_000_000 * 2; // still int multiply → overflow, then widen long z = 2_000_000_000L * 2; // correct
Use Math.addExact / multiplyExact / toIntExact when wrap is a defect (sizes, money in cents).
char is not “a character” in the Unicode sense. It is a code unit. Supplementary characters need two chars. Prefer int code points when you mean a Unicode scalar.
Each primitive has a wrapper: Boolean, Byte, Short, Character, Integer, Long, Float, Double. Collections, generics, and null need wrappers.
Integer i = 5; compiles to Integer.valueOf(5).int n = i; compiles to i.intValue() and NPEs if i is null.Integer.valueOf to cache −128..127. Other ranges are optional. == on Integers is a trap.new Integer(5) is deprecated. Use valueOf or autoboxing.Integer a = 127; Integer b = 127; Integer c = 128; Integer d = 128; a == b; // true (cache) c == d; // often false — never write this a.equals(b); // true — the correct check Integer boxed = null; int n = boxed; // NullPointerException
Boolean: Boolean.TRUE / FALSE. if (flag) unboxes; a null Boolean flag NPEs. Prefer primitive boolean fields.
Performance: boxing in a hot loop allocates. List<Integer> of a million numbers is a million objects. Use arrays of primitives or specialized collections when you have measured the cost.
Mental model: a reference variable is a named slot that holds an address or null. Assignment copies the address. Two names, one object.
int[] a = {1};
int[] b = a;
b[0] = 9; // a[0] is 9
b = new int[]{0}; // a unchanged
null is a value of every reference type. It is not an object. You cannot call methods on it (NPE).instanceof is false for null (and does not throw). Pattern matching if (x instanceof String s) (Java 16) also fails safely on null.// Incorrect
if (obj.getClass() == String.class) { } // NPE if obj is null
// Correct
if (obj instanceof String s) { use(s); }
int → long → float → double is implicit. int → float can lose low bits of large ints.(byte) 256 is 0.ClassCastException. Prefer instanceof / pattern matching.var (Java 10) is still statically typed. The compiler infers the type from the initializer. It is not JavaScript var. Do not use it when the type is the documentation (var x = get()).a + b: byte/short/char become int before arithmetic.byte b = 1; b = (byte) (b + 1); // + yields int b += 1; // compound assignment includes a hidden cast
An array is a fixed-length object. length is a final field. Elements default like fields. Covariance: a String[] is a Object[] — storing a non-String throws ArrayStoreException at runtime. That is why generic collections are invariant.
Object[] xs = new String[1]; xs[0] = Integer.valueOf(1); // ArrayStoreException
int[][] is an array of arrays (rows can have different lengths).List for most APIs; use arrays for primitives, JNI, or measured hot paths.String is a reference. Literals are interned. See the String Handling topic.Class<T> objects are references: String.class, int.class, Integer.TYPE.== is correct for enums; equals also works.new Point(1,2) is a distinct object unless you intern them yourself.== on wrappers (Integer, Long) except for enum/Boolean.TRUE style singletons you control.list.get(i) + 1 or conditions.int multiply with a long result — widen first.float for money. Using double as a HashMap key (NaN, −0.0).char is a user character.boolean vs Boolean in JSON/beans — three states (true/false/null) when you meant two.// Incorrect
if (count == status) { } // int vs Integer may unbox NPE; or == on Integers
// Correct
if (Objects.equals(count, status)) { }
if (count != null && count == 3) { } // unbox only after null check
== boxed numbers. Know the −128..127 cache so interview tricks do not become production style.instanceof.var is inferred static typing — keep the type visible when it matters.~ Good luck with your revision! ~