World 2 of 26 · Beginner

📦 Variables & Types

16 Java concepts, each explained step by step with real code, a common trap and practice questions.

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