Data Types
A variable is a named memory location that can hold data.
Why the Program Must Know the Kind of Data
Imagine being asked to store the item 17. Before a program can work with it, the program needs to know what kind of data it is handling. Is it a whole number, a number with a decimal point, a single symbol, or a sequence of text? A data type supplies that category. Throughout this article, you will trace how a value is named, located, and classified, so that the idea of a data type becomes concrete.
What do you think happens?
Are the written values 17 and 17.0 the same kind of data to a computer?
Reveal answer
No. 17 is an integer and 17.0 is a floating-point number. The computer treats them as different kinds of data and represents them differently.
The visible difference is small, but the computer classifies the two values as different data types. The rest of this article explains why.
A Name for a Memory Location
A variable is a named memory location that can hold data. The name identifies the location, and the location holds the data.
Programs need a way to refer to the data they use. A variable provides that way: it is a memory location given a name. Because the location has a name, the program can refer to the data through that name rather than needing to know the location itself.
The diagram shows the three parts of the relationship. The name score identifies a location. That location holds the value 17. The name and the location are not the same thing: the name is how the program refers to the storage, and the location is the storage that actually holds the data.
Why Programmers Use Names and the Compiler Tracks Locations
The programmer refers to a variable by its name, not by its memory location. This keeps the programmer focused on meaningful names such as score rather than on the physical location of the storage.
The compiler is responsible for keeping track of the memory location associated with that name. The programmer writes the name, and the compiler arranges for that name to refer to the correct storage. The name is the part the programmer works with; the location is the part the compiler manages.
Following the name score
A program needs to keep track of a player's score. Trace how the name relates to the storage.
- 1Choose a meaningful name
The programmer names the variable score, which describes what the data means.
- 2Associate the name with a location
The compiler keeps track of the memory location associated with the name score.
- 3Store the data
The location holds the data, here the value 17.
- 4Refer by name
Later code uses score to refer to the data, without the programmer needing to know where the location is.
The programmer works with the meaningful name score, while the compiler maintains the connection between that name and the location that holds the data.
What a Data Type Tells the Program
Data types tell a program what sort of data a variable can hold. In programming, a variable has a type that indicates what sort of data it can hold.
The important step is to classify the value before thinking about how a variable might use it. The type is the category that the program uses to interpret what is stored in the location. The computer uses different representations for different data types, so the type determines how the stored data is handled.
Numbers: Integers and Floating-Point Values
Two common categories of numeric data are distinguished in this material. Integers and floating-point numbers are distinct, even when their written values appear closely related.
Why 17 and 17.0 Are Different Data
The visible difference between 17 and 17.0 is small, but the computer treats them as different kinds of data: the first is an integer and the second is a floating-point number. The integer 17 and the floating-point number 17.0 belong to different data types and are represented differently inside the computer.
A programmer may see two numbers that look almost identical, but the computer does not treat every written number as the same kind of value. This distinction is one reason a variable's type matters: it tells the program what sort of data is being stored.
Characters and Strings
Characters are a different kind of stored data from numbers. A character represents an individual symbol or letter. A string goes further: it is a sequence of characters and can therefore contain multiple characters.
In the diagram, the single character A is one symbol. The string Hello is a sequence of five characters, shown here in order. The source describes a string as a sequence of characters that can contain multiple characters; the ordering of its characters is what distinguishes it from a single symbol.
Mistakes Beginners Make with Data Types
Common mistake Treating 17 and 17.0 as interchangeable because they look nearly the same
What it looks likeAssuming that a value written as 17.0 is just another way of writing 17, so it can be stored and used in exactly the same way
Why it failsThe integer 17 and the floating-point number 17.0 belong to different data types and are represented differently inside the computer.
Do this insteadClassify the value first: a whole number is an integer, and a number written with a decimal point is a floating-point number.
Common mistake Confusing a variable's name with its memory location
What it looks likeThinking that score is the storage itself, rather than the name that identifies the location holding the data
Why it failsThe name identifies the location, while the location holds the data. The programmer refers to the variable by name, and the compiler tracks the location.
Do this insteadThink of score as the label, the memory location as the storage behind it, and 17 as the data held there.
Common mistake Treating a string as if it were a single character
What it looks likeAssuming that Hello is just one symbol because it is a word
Why it failsA character is one individual symbol or letter, while a string is a sequence of characters and can contain multiple characters.
Do this insteadAsk whether the data is one symbol or a sequence of symbols before deciding how it is classified.
Common mistake Forgetting that a data type tells the program what sort of data a variable can hold, and so skipping the step of classifying the value first
Practice: Classifying Stored Values
For each value below, decide whether it is best classified as an integer, a floating-point number, a character, or a string: 42, 42.0, a, and cat. Then explain what the classification tells a program about the data.
Hints
- Integers cover whole-number values, including negative values and zero.
- Floating-point numbers are written with decimal points.
- A character is one individual symbol or letter; a string is a sequence of characters.
Key Takeaways
- A variable is a named memory location that can hold data: the name identifies the location, and the location holds the data.
- Programmers refer to variables by meaningful names rather than by memory location, and the compiler is responsible for tracking the location associated with each name.
- A data type tells a program what sort of data a variable can hold, and the computer uses different representations for different data types.
- Integers (whole numbers, including negatives and zero) and floating-point numbers (written with decimal points) are distinct, so 17 and 17.0 are different kinds of data.
- A character is one individual symbol or letter, while a string is a sequence of characters that can contain multiple characters.