Primitive types.

In the previous video, we learned that types classify data and determine what operations are valid.

Now we look at the most basic types — the primitives. These are the building blocks from which all other data is constructed.

Integers. Floating-point numbers. Booleans. Characters. These are the atoms of data.

Primitive types are the simplest, most fundamental types in a programming language.

They are not composed of other types. They are the foundation. All complex data structures are ultimately built from primitives.

Why do we have primitives? Because computers work with simple values directly. The CPU can add two integers in a single operation. It can compare two numbers instantly.

Primitive types map closely to what hardware can do efficiently. They are fast. They are simple. They are the building blocks.

Different languages have slightly different primitives. But most share the core ones: numbers, booleans, and characters.

Integers represent whole numbers. Numbers without fractional parts.

One. Forty-two. Negative seven. One million. These are all integers.

Integers are exact. There is no approximation. The number 42 is exactly 42, not 41.9999 or 42.0001.

This exactness is important. When counting things — users, items, iterations — you want exact values. You cannot have 2.5 users. You have 2 or you have 3.

Integers have a range. They are stored in a fixed number of bits. Common sizes are 8, 16, 32, and 64 bits.

A 32-bit integer can hold values from about negative two billion to positive two billion. Need bigger numbers? Use 64-bit integers or a special big-number type.

If a calculation exceeds the range, overflow occurs. The value wraps around or produces an error. Understanding integer limits matters for correctness.

Floating-point numbers represent decimal values. Numbers with fractional parts.

Three point one four. Negative zero point five. One thousand point zero. These are floating-point values.

Why "floating-point"? Because the decimal point can "float" to different positions. This allows representing very large and very small numbers with the same amount of storage.

Floating-point numbers are approximations. Unlike integers, they cannot represent all values exactly. The number 0.1 cannot be represented precisely in binary floating-point.

This has consequences. Adding many floating-point numbers can accumulate small errors. Comparing floating-point values for exact equality is dangerous.

For money, measurements, and other precision-sensitive data, be careful with floating-point. Sometimes integers or specialized decimal types are better choices.

Common floating-point sizes are 32 bits (single precision) and 64 bits (double precision). Double precision offers more accuracy and range.

Booleans represent truth values. True or false. Nothing else.

Is the user logged in? True or false.

Is the number positive? True or false.

Did the operation succeed? True or false.

Booleans are the foundation of logic in programs. Every condition, every branch, ultimately evaluates to a boolean.

The if statement checks a boolean. The while loop checks a boolean. Comparisons produce booleans.

Though simple, booleans are essential. They are how programs make decisions. Every yes/no question in a program uses booleans.

A boolean needs only one bit theoretically. In practice, languages often use a full byte for memory alignment reasons. But conceptually, a boolean is the smallest meaningful piece of data.

Characters represent individual symbols. A single letter, digit, or punctuation mark.

'A'. 'z'. '7'. '@'. '!'. Each is a character.

Characters are the atoms of text. A string is a sequence of characters. When you work with text, you work with characters.

How are characters stored? Each character maps to a number according to an encoding.

In ASCII, 'A' is 65. 'a' is 97. '0' is 48. The encoding defines the mapping.

Modern systems often use Unicode, which can represent characters from almost all writing systems. Letters, symbols, emoji — all have Unicode code points.

A character type might be 8 bits (ASCII), 16 bits (many languages), or 32 bits (full Unicode). The size determines what characters can be represented.

Understanding character encoding matters when working with text from different languages and systems.

Each primitive type has a fixed size in bits. This size determines the range of values.

For integers: 8 bits gives you negative 128 to positive 127. 16 bits gives you negative 32,768 to positive 32,767. 32 bits gives you roughly negative 2 billion to positive 2 billion. 64 bits gives you a huge range, roughly plus or minus 9 quintillion.

For floating-point: 32 bits gives about 7 decimal digits of precision. 64 bits gives about 15 decimal digits of precision.

Why does this matter? Because choosing the wrong size can cause bugs.

If you use an 8-bit integer for a count that might exceed 127, you will get overflow. If you use single-precision floating-point for financial calculations, you might lose precision.

Choosing the right size is part of designing correct programs. Not too small, or you risk overflow. Not unnecessarily large, or you waste memory.

Each primitive type supports specific operations.

Integers support arithmetic: addition, subtraction, multiplication, division, remainder. They support comparison: equal, not equal, less than, greater than.

Floating-point supports the same arithmetic and comparison. But remember the precision issues.

Booleans support logical operations: and, or, not. They support comparison for equality.

Characters support comparison based on their encoding order. 'a' is less than 'b' because 97 is less than 98.

These operations are fundamental. Everything else is built on top of them. When you add two large numbers, the CPU is ultimately performing primitive integer operations.

Understanding what operations each primitive supports helps you choose the right type and avoid errors.

Sometimes you need to convert between primitive types.

Converting an integer to a floating-point: usually safe, but might lose precision for very large integers.

Converting a floating-point to an integer: the fractional part is lost. 3.7 becomes 3 or 4, depending on rounding rules.

Converting a small integer to a larger integer: always safe. All values fit.

Converting a large integer to a smaller integer: dangerous. Values might not fit, causing overflow or truncation.

These conversions are called casts or coercions. Some languages require explicit casts. Others allow implicit conversion in some cases.

Being explicit about conversions makes code clearer and helps avoid surprises. When you convert, you acknowledge that something might change.

How do you choose which primitive to use?

For whole numbers: use integers. Choose a size that fits your range with some margin.

For measurements, scientific values, or calculations with fractions: use floating-point. Use double precision unless memory is very constrained.

For yes/no values: use booleans. Do not use integers (0/1) to represent booleans — it obscures intent.

For single characters: use the character type. For text, use strings (which we will cover later).

When in doubt, prefer larger sizes. Memory is usually cheap. Overflow bugs are expensive.

But do not overthink it. For most cases, the default integer and floating-point types are fine. Worry about size when you have a specific reason to.

Let us summarize.

Primitive types are the fundamental building blocks of data.

Integers represent whole numbers. They are exact but have a limited range.

Floating-point numbers represent decimals. They have wide range but limited precision.

Booleans represent true or false. They are the foundation of program logic.

Characters represent single symbols. They are the atoms of text.

Each primitive has a size that determines its range and precision.

Primitives support specific operations: arithmetic, logic, comparison.

Conversions between primitives are possible but may lose information.

Choose the right primitive for your data. Consider the range and precision you need.

These simple types are the foundation of all data in programs.