How to use primitive data types

Store whole numbers

int is the usual whole-number type. Use an L suffix for a long literal beyond the int range.

int students = 30;
long worldPopulation = 8_000_000_000L;

Store decimal numbers

double is the normal floating-point type. A float literal needs an F suffix.

double price = 19.95;
float ratio = 0.5F;

Store characters and booleans

A char holds one UTF-16 code unit in single quotes. A boolean is either true or false.

char grade = 'A';
boolean enrolled = true;

Know the primitive families

  • Integers: byte, short, int, and long.
  • Floating point: float and double.
  • Other primitives: char and boolean.
  • String is a class, not a primitive type.