Concepts / Data Types

Data Types

A variable is a named memory location that can hold data.

  • Programming

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?

  • Yes, they are the same kind of data

  • No, one is an integer and the other is a floating-point number

Reveal answer
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

Definition

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.

scorevariable namememory locationholds the data17stored dataidentifiesholds
How does the name score relate to the memory location, and to the value the location holds?

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.

Worked example

Following the name score

A program needs to keep track of a player's score. Trace how the name relates to the storage.

  1. 1
    Choose a meaningful name

    The programmer names the variable score, which describes what the data means.

  2. 2
    Associate the name with a location

    The compiler keeps track of the memory location associated with the name score.

  3. 3
    Store the data

    The location holds the data, here the value 17.

  4. 4
    Refer by name

    Later code uses score to refer to the data, without the programmer needing to know where the location is.

Result

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

Definition

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.

17integer data type17.0floating-point data typelook alike, different types
How do the integer 17 and the floating-point 17.0 differ in kind, even though they look nearly identical?

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.

Aone symbolHcharacter 1Helloa sequence of charactersecharacter 2lcharacter 3lcharacter 4ocharacter 5containscontainscontainscontainscontains
How is a single character stored differently from a string of several characters, which occupies a sequence of positions?

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 like

    Assuming 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 fails

    The integer 17 and the floating-point number 17.0 belong to different data types and are represented differently inside the computer.

    Do this instead

    Classify 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 like

    Thinking that score is the storage itself, rather than the name that identifies the location holding the data

    Why it fails

    The 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 instead

    Think 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 like

    Assuming that Hello is just one symbol because it is a word

    Why it fails

    A character is one individual symbol or letter, while a string is a sequence of characters and can contain multiple characters.

    Do this instead

    Ask 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

EASY

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.