📦 Variables & Types · Beginner

The 8 primitive types in Java

byte, short, int, long, float, double, char, boolean and their sizes.

🧩 The mysteryEvery Java program, from games to banks, is built from just eight tiny value types. Can you name all eight?

Meet the eight

These are Java's primitive types: simple values, not objects. Everything else (String, arrays, your own classes) is a reference type, which you'll meet soon.

byte  short  int  long  // whole numbers
float  double           // decimals
char                    // one character
boolean                 // true / false

Whole numbers, doubling sizes

A bit is a single 0 or 1. byte uses 8 bits, short 16, int 32, long 64: each size doubles. int is the everyday default; long is for values beyond about ±2.1 billion.

byte   8 bits
short 16 bits
int   32 bits
long  64 bits

Decimals, chars, booleans

float is 32 bits and double is 64 bits (the default for decimals). char is 16 bits and holds one UTF-16 character unit; it's unsigned, from 0 to 65,535. boolean is simply true or false.

🤔 Think first

Odd one out

byte, int, char, String. Which one is not a primitive type?

Think about it, then reveal the answer

String. It's a class, so a String variable holds a reference to an object. All eight primitive type names are lowercase.

🔮 Predict it

Your turn

Which 16-bit type can never hold a negative number?

  1. short
  2. char
Show the answer

char, Java's only unsigned primitive. It stores UTF-16 code units, which are numbered from 0 upward. short is also 16 bits, but signed: −32,768 to 32,767.

⚠️ The trap

Coming from C?

In C, an int might be 16, 32 or 64 bits depending on the machine. Java fixes every primitive's size in the language specification: an int is always 32 bits, on every CPU and every OS. That's part of what makes Java portable.

💼 In the real world

In real projects

Type choice is a memory decision at scale. An array of 10 million int values takes about 40 MB; the same in long takes about 80 MB. Databases, games and network protocols pick types to fit their data.

Key takeaways

  1. Integers: byte 8, short 16, int 32, long 64 bits
  2. Floating point: float 32, double 64 bits
  3. char: 16-bit unsigned; boolean: true/false
  4. Sizes never depend on the CPU or OS
🤯 Did you know?

The spec doesn't fix a size for boolean. JVM instructions treat booleans as ints (0 or 1), and Oracle's JVM stores boolean arrays using one byte per element.

Practice questions

Which one is NOT a primitive type?

  1. int
  2. String
  3. boolean
  4. char
Check your answer

String. String is a class, so a String variable holds a reference to an object. The other three are primitives.

Which primitive type is 16 bits and can never be negative?

  1. short
  2. char
  3. byte
  4. boolean
Check your answer

char. char is Java's only unsigned primitive, ranging from 0 to 65,535. short is also 16 bits but signed.

Next: what happens when a number gets too big for its box? Spoiler: Java doesn't complain. At all.