Java Tutorial · Revision notes

Java Variables & Data Types

Variables store data, data types decide what kind of data a variable can hold, casting converts between types, operators perform actions and keywords are reserved words with special meaning. This chapter covers all five with complete programs.

Quick revision

  • Variable kinds: local (no default, stack), instance (default value, heap), static (one copy per class).
  • 8 primitives: byte 1, short 2, int 4, long 8, float 4, double 8, char 2 bytes, boolean true/false.
  • Whole-number literals are int and decimal literals are double; use L and f suffixes.
  • Widening is automatic; narrowing needs a cast and may lose data.
  • byte, short and char are promoted to int in arithmetic.
  • Integer division truncates; && and || short-circuit; i++ uses then changes, ++i changes then uses.
  • Java has 51 reserved keywords; const and goto are unused; true, false and null are literals.
  • Use BigDecimal for money and equals() for strings.
Read the full Java Variables & Data Types notes on Doubtclip →