Beginner Topic 2 of 54 Programming Fundamentals
Data Types
Classifications such as integer, float, boolean and string that define valid operations.
How it works
Each data type fixes how a sequence of bits is interpreted and which operations are legal on it. Integers perform exact whole-number arithmetic within a fixed range, floating-point numbers approximate real numbers using sign, exponent and mantissa, booleans hold true or false, and strings hold sequences of characters. Static type systems check these rules at compile time, dynamic ones check them as the program runs, and strong typing limits implicit mixing of types.
Step-by-step walkthrough
- An int holds an exact whole number.
- A float approximates a real number using sign, exponent and mantissa.
- A bool is only true or false and drives every decision.
- A string is a sequence of characters.
- The type decides which operations are legal, for example 42 + 3.14 works but 42 + "hi" does not.
Where it is used
Choosing between int, float and decimal matters for money, identifiers and measurements, and booleans drive every decision in code. Databases, file formats and network protocols all define their own types that must be mapped onto programming-language types correctly.
Common pitfall
Floats cannot represent 0.1 exactly, so 0.1 + 0.2 is not equal to 0.3 and equality tests on floats fail. Integers overflow silently in many languages. Never use binary floats for currency; use integers of cents or a decimal type, and compare floats with a tolerance.
Where it fits in the course
Data Types is topic 2 of 54 in Programming Fundamentals, rated Beginner. It follows Variables. The next topic is Operators.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Programming Fundamentals quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Data Types?
Where do people use Data Types?
What is the most common mistake with Data Types?
What should I learn before this topic?
How can I practice it?
Is Data Types hard to learn?
How long does it take to learn Data Types?
Which topics are related to Data Types?
What should I study after Data Types?
Related topics
- Variables (Programming Fundamentals)
- Operators (Programming Fundamentals)
- Expressions (Programming Fundamentals)
- Type Casting (Programming Fundamentals)
- Data Types in SQL (Databases)
- Algebraic Data Types (Languages & Compilers)
- Open Data and Licensing (Emerging & Frontier Topics)