Basic data types

The bricks of everything · Part of Complexity and data

Key idea

Practice

6 questions · about 3 min

Explained step by step

Data type

Basic data types are the ones the processor can handle directly, and everything else is built from them: text, images, video. There are 4 families. Integers, like int, store numbers without a decimal point. Floating-point types, like float and double, store numbers with decimals. The character, char, stores a letter, a sign or an emoji. The boolean, bool, stores yes or no. A photo, in the end, is millions of integers that say the color of each dot.

Each language defines its own types, with sizes and ranges that can differ. In Python, an int grows as much as memory allows, so it doesn't overflow. Python also has no char: a character is a text of length 1. In C, a char takes 1 byte; in Java, 2. And in C sizes depend on the platform. That's why, when a number is very big or memory matters, you have to check what your language does.

The data type and the data structure answer different questions. The type says what's stored: an integer, a decimal, a letter. The structure says how it's organized: in a line, in a tree, in a table. The same array can hold integers, decimals, letters or booleans; the structure doesn't change, the contents do. It's like a shelf: the shelf is the structure and what you put on it, books or jars, is the type.

Byte

The computer stores everything in binary, with 0s and 1s. Each 0 or 1 is a bit. 8 bits make 1 byte, the unit memory is measured in. With n bits you can write 2ⁿ different values: with 1 bit, 2; with 8 bits, 256; with 32 bits, over 4 billion. It's like a combination lock: with 3 wheels from 0 to 9 there are 1,000 combinations; with wheels that only have 0 and 1, each new wheel doubles the combinations.

Integer

A type's range, that is which values it can store, depends on how much space it takes. In Java, byte takes 1 byte and goes from −128 to 127: 256 values. int takes 4 bytes and goes from about −2.1 billion to 2.1 billion. long takes 8 bytes and reaches about 9 quintillion. If a value goes past the range, the number "wraps around" and an absurd result appears, like a counter jumping from 127 to −128.

Boolean

A boolean only needs 1 bit, because it's true or false. But it almost always takes a whole byte, 8 bits. The reason is that the processor can't fetch a loose bit: the smallest unit it can point to with an address is the byte. It's like a hotel where the rooms have 8 beds: even if you travel alone, you get the whole room, because single beds aren't rented out.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

The bricks of everything