Data types

A data type specifies a particular kind of data.

Primitive types are the most basic data types available. In Java, the built-in primitive types include int (to store an integer), double (to store a decimal number), char (to store a character) and boolean (to store either true or false). In Java, primitive types are always spelled with lowercase first letters.

Java also has object types (also called reference types). These are more complex types that usually represent more complex entities and are built up from primitive types. The template for an object type is called a class. Some object types are available in class libraries, for example the String type is built from a sequence of char types and is used to represent text. Other object types are defined by the programmer when they write a class to represent a real-world object. By convention, object type names start with an uppercase first letter.

All data types have legal values of those types. For example, 17 is a legal value of the primitive type int in Java. A legal value of the String type in Java is "Brian".

When a variable of a primitive data type is declared, a memory location of the correct size for that datatype is created at runtime. When a value is assigned to the variable, it is stored directly in the memory location, so

int myNumber = 17;

causes an area in memory (exactly big enough to store an int) to be labelled myNumber and 17 to be stored directly in it.

The figure shows the variable name myNumber written next to a box containing the number 17.

A reference type is stored differently. When a variable is declared to be of a reference type the memory location associated with it at runtime is of exactly the right size to hold a memory address. This means that an object, whose size may not be known in advance, can be stored anywhere in memory and only its location, or memory address, needs to be stored in the memory space associated with the variable. This gives more flexibility in allocating memory whilst the program is running.

So, the result of executing the following lines of Java code

String myName;

myName = "Brian";

can be represented as follows:

The figure shows the variable name myName written next to an arrow pointing at a series of connected boxes containing the characters 'B', 'r', 'i', 'a' and 'n'. The boxes are labelled with the numbers 0, 1, 2, 3 and 4.

In this case, myName contains the memory address of where the String object (which itself consists of 5 primitive char values) is stored.

Comparing Java with Python

In Java there are both primitive types and object types, which are stored in different ways. Although Python also provides data types for numeric, Boolean and string data, all types in Python are object types: there are no primitive types. This means that (unlike in Java) a Python variable always contains the memory address of where the object is located.

In Java, a String must always be enclosed in double quotes (single quotes are reserved for char data), whereas in Python a string can be indicated using either double or single quotes.

Test your understanding

  1. The data type in the declaration of a variable determines what kind of data can be assigned to the variable. True or false?
  2. In Java, a data type is either a primitive type or an object type. True or false?
  3. A String object is stored directly in a variable. True or false?