Skip to content

Integer types (byte, short, int, long)

Java provides four integer types to represent whole numbers. Choose the smallest type that fits your range to save memory.

TypeSizeRangeUse case
byte1 byte-128 to 127small counters, file I/O, network data
short2 bytes-32,768 to 32,767rarely used, mostly for compatibility
int4 bytes-2³¹ to 2³¹‑1default integer type, most common
long8 bytes-2⁶³ to 2⁶³‑1large numbers (e.g., timestamps, large counts)

Literals:

  • Decimal: int i = 100;
  • Octal (prefix 0): int octal = 012; // decimal 10
  • Hexadecimal (prefix 0x): int hex = 0x1A; // decimal 26
  • Binary (prefix 0b): int bin = 0b1010; // decimal 10
  • Underscores: int million = 1_000_000;

Long literals must end with L or l:

long big = 2_147_483_648L; // beyond int range

Operations:

int a = 10, b = 3;
int sum = a + b; // 13
int product = a * b; // 30
int division = a / b; // 3 (integer division truncates)
int remainder = a % b; // 1

Be careful with overflow: if the result exceeds the range, it wraps around silently.

int max = Integer.MAX_VALUE; // 2_147_483_647
int overflow = max + 1; // becomes -2_147_483_648