📦 Variables & Types
16 Java concepts, each explained step by step with real code, a common trap and practice questions.
- Declaring & initializing variablesType + name, assignment, definite assignment rule for locals.
- The 8 primitive typesbyte, short, int, long, float, double, char, boolean and their sizes.
- Integer ranges & overflowFixed ranges, silent wrap-around, Math.addExact for checked arithmetic.
- LiteralsDecimal, hex 0x, binary 0b, octal 0, underscores, L and f/d suffixes, char literals.
- Widening conversionsAutomatic safe conversions (int → long → float → double) and precision loss in long → float.
- Narrowing castsExplicit (int) casts, truncation of decimals and bits lost on overflow.
- Numeric promotion in expressionsbyte/short/char promote to int in arithmetic; mixed types promote to the wider type.
- char and Unicodechar is a 16-bit UTF-16 code unit; char arithmetic and code points.
- booleanOnly true/false; no conversion to or from numbers.
- Constants with finalfinal variables, static final constants and naming.
- Local type inference with varvar infers the type at compile time; still statically typed; where it is not allowed.
- Primitives vs referencesPrimitives hold values; reference variables hold a pointer to an object or null.
- Wrapper classesInteger, Double, Boolean…, parsing (Integer.parseInt) and valueOf.
- Autoboxing & unboxingAutomatic conversion, NullPointerException when unboxing null.
- Integer cache trapInteger.valueOf caches -128..127 so == on boxed values is unreliable.
- Default valuesFields get defaults (0, false, null); local variables do not.