Skip to content

Bits and Bytes

The smallest unit of memory Java uses is a byte.

A byte is 8 bits, each set to 0 or 1.

Bits in a byte

Variables and Memory

When we declare a variable, Java reserves enough memory for that variable type.

Assigning a value to a variable puts 0's and 1's into that memory.

Below we declare an int, which uses 4 bytes, and give it the value 1.

int x = 1;

Bits in memory

Memory Sizes You May Have Seen

1,024 bytes is a kilobyte (KB).

1,024 kilobytes is a megabyte (MB).

1,024 MB is a gigabyte (GB).

Why 1024? Because 1024 is 210, and powers of 2 matter in binary (0's and 1's).

Practice Exercise

Technically a kilobyte (KB) is a power of 10, so 210 is actually a kibibyte (KiB). Megabyte (MB) is also a power of ten, so 220 is a mebibyte (MiB).

You can piece together how the names relate.

kibi = kilo - lo + bi (for binary)

mebi = mega - ga + bi


Prev -- Up -- Next