Uni-Sine
Computer ScienceProgramming and algorithms

Data types in programming

A data type tells a program what kind of value a piece of data is, which decides how it is stored and what you can do with it. The five basic data types in programming are integer (whole numbers), real or float (numbers with a decimal point), Boolean (true or false), character (one symbol) and string (text). You can add two integers, but adding two strings joins them, so the type changes what the same operation does.

Those five are the ones the AQA GCSE Computer Science specification lists, noting that languages may call them by other names. The table shows the names Python and JavaScript use.

The main data types

Data typeHoldsExamplesPythonJavaScript
IntegerWhole numbers, positive or negative42, 0, −7intnumber
Real (float)Numbers with a fractional part3.14, −0.5, 2.0floatnumber
BooleanOne of two valuesTrue, Falseboolboolean
CharacterA single letter, digit or symbol'A', '7', '?'str of length 1string of length 1
StringA sequence of characters: text"Hello", "SW1A 1AA", ""strstring

Integer

Use an integer for anything you count: scores, ages, the number of items in a basket. Integer arithmetic is exact. Python integers can be as big as memory allows, while many other languages give each integer a fixed number of bits, which limits how big it can get.

Real, or floating point

Use a real (called a float or double in most languages) for measurements such as height, temperature or speed. Reals are stored in binary floating point, which cannot represent most decimals exactly. Try this in Python or the Code Editor:

console.log(0.1 + 0.2);          // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3);  // false

The error is tiny, but it means you should never test two reals for exact equality, and you should not store money as a float. Store prices as a whole number of pence (or cents) in an integer instead: £19.99 becomes 1999.

Boolean

A Boolean has only two values, true and false, and is what every condition in an if statement or while loop produces. Name Boolean variables as yes or no questions, such as isLoggedIn or has_paid, so code reads clearly.

Character and string

A character is one symbol, stored as a number using a character set such as ASCII or Unicode: in both, "A" is 65. A string is a sequence of characters, so it has a length and you can pick out characters by position. Python and JavaScript have no separate character type; a character is just a string of length 1.

Choosing the right data type

Ask what values the data can take and what you will do with it. Here is a record for one student.

DataBest typeWhy
NameStringText of any length
Age in yearsIntegerA whole number you might compare or add to
Height in metresRealA measurement with a fractional part, such as 1.68
Has paid for the tripBooleanOnly yes or no
Form group letterCharacterExactly one symbol, such as B
Phone numberStringIt can start with 0, may contain + or spaces, and you never do arithmetic with it

The phone number is the classic exam trap. It is made of digits, but storing it as an integer would drop the leading zero, and adding two phone numbers means nothing. The same goes for postcodes, PINs and card numbers.

Converting between data types: casting

Changing a value from one type to another is called type conversion, or casting when you do it on purpose. Anything typed by a user arrives as a string, so you often need to convert it before doing maths.

age = int(input("Age: "))   # "15" becomes 15
price = float("4.50")       # 4.5
label = str(42) + "kg"      # "42kg"
whole = int(3.9)            # 3: int() cuts off the decimal part, it does not round

Some conversions happen automatically, which is called implicit conversion. Adding an integer to a real gives a real in most languages: 5 + 3.2 is 8.2. Explicit conversions such as int(3.9) can lose information, so check whether you want to cut off the decimal part or round it.

Primitive and composite data types

The five types above are primitive: single values built into the language. Composite types are built from them. An array or list holds many values in order, a record or object groups named fields (such as the student above), and in languages such as C a pointer holds the memory address of another value. Python and JavaScript treat strings as a basic type, while in C a string is an array of characters.

Static and dynamic typing

Languages also differ in when they check types. In a statically typed language such as Java, C or TypeScript, each variable has a fixed type, and mistakes are caught before the program runs. In a dynamically typed language such as Python or JavaScript, a variable can hold any type and change it, and type errors only show up when that line runs.

value = 10
value = "hello"     # fine: the variable now holds a string
result = value + 5  # TypeError when this line runs

Sort some real languages by how they run and how they check types, then press Mark to check your answer in the course:

Sort languages into compiled/interpreted, then static/dynamic bins.

Press Mark to have your answer marked in the Programming Languages course.

Common mistakes

  • Storing phone numbers, postcodes or IDs as integers. Leading zeros vanish and the symbols do not fit. Use a string.
  • Forgetting that input is a string. Convert it with int() or float() before doing arithmetic.
  • Using a float for money. Rounding errors creep in. Count pence in an integer.
  • Comparing reals with ==. Check whether they are close enough instead.
  • Thinking int() rounds. In Python, int(3.9) is 3. Use round() to round.

Common questions

What are the 5 main data types?
Integer, real (also called float), Boolean, character and string. These are the five the AQA GCSE Computer Science specification lists. Many languages add more, such as separate types for very large numbers or for dates.
What is the difference between an integer and a real?
An integer is a whole number, such as 12, and integer arithmetic is exact. A real is a number with a fractional part, such as 12.5, stored in floating point, which can introduce tiny rounding errors.
Is a string a primitive data type?
It depends on the language. Python and JavaScript treat strings as a basic built-in type. In C, a string is an array of characters, so it is a composite type.
What data type should a phone number be?
A string. Phone numbers can start with 0, can contain + or spaces, and are never used for arithmetic, so an integer would lose information.
What is casting in programming?
Converting a value from one data type to another on purpose, such as int("42") to turn the string "42" into the integer 42, or str(42) to turn it back into text.
Does JavaScript have an integer type?
Its main number type, number, is a floating-point type used for both whole numbers and decimals. Whole numbers are exact up to 9,007,199,254,740,991, which is 2⁵³ − 1. For bigger whole numbers it has a separate BigInt type.

Revision cards

Say the answer first, then flip the card to check it.

Where to go next

  • Solve it

    Code Editor

    Write and run code right in your browser

    Open the tool
  • Learn it

    Programming Languages

    Compiled vs interpreted, paradigms and the evolution of how we talk to machines

    Start the course
  • Test it

    GCSE Computer Science

    A new exam-style paper every day, with instant marking and a mark scheme

    Start today's paper
Read next
Computer Science · Programming and algorithmsConditional statements in programming: if, else and logicHow conditional statements in programming work: if, elif and else, comparison operators, AND, OR and NOT, truth tables, with Python and JavaScript code.