Delphi Programming/Data types

First, let's define a type.

In Delphi, like in C, VB and unlike PHP, Python or other modern languages, a variable is typed and can only contain values of this type. To make it simple, a type is a variable attribute.

Basic types
Delphi is extremely complete for this. There are lots of predefined types and you can also create your own types (we will see it later). Here is a table with the most useful basic types (also called primitive types):


 * Remarks: Above are only the most popular predefined types. There are dozens of other types just for the integers (smallint, longint, int64...) whose usage fits very specific needs (like the size of the data we will put in) and so optimizes the memory usage. For more information about it, see the documentation in the Delphi help content.

char
A char (a character) is something like '#', 'A', '2', ';', '.', '?' and so on. It has a number in a library of chars, the ASCII. In Delphi, a char is typed between two single quotes. If the given character is the single quote itself, you should double the character, in other words, you have to type four single quotes.

string
You have already worked with strings in the last chapter, such as 'Hello World' or 'Hi there' and so one. A string is a sequence of chars. And then write between begin and end: If you want to write a single quote in a string, the best way is to type two successive single quotes:

Strings behave like the tables. You can access to a character using an index: The first index is 1 (and not 0).

Textual expressions
Strings can be linked using the + operator:

The + operator can be used with variables with different types. According to the types, the result will have a different type:
 * string + string → string
 * string + char → string
 * char + char → string
 * char + string → string
 * string + number → error
 * number + string → error
 * number + number → number
 * number + char → error
 * char + number → error

Integers
Integers are even integers, you write:

Floats
Floats are decimal numbers. They use a floating point. You can write: An Extended is more precise than a Real, but needs more main storage.

Boolean
Boolean is a logical value. It can be 'True' or 'False'. Write: But there is only a sense of Boolean when you know the conditional structures in the next chapter.

Arrays
An array is a collection of strings, floats, integers or any data type. It can also contain an array. In this case, we have an array with several dimensions. You say: for one dimension. For two: and so on. A single element of an array is for example:

Record
A record (also called a structure in other languages) represents a set of heterogeneous data. Each item is a field. The record type declaration specifies the name and the type of each field. By convention, the name of a record type starts with a "T". A record type declaration has the following syntax:

For example, the following declaration creates a record type called TDateRec.

Each TDateRec contains three fields: an integer value called Year, an enumerate value called Month and another integer value between 1 and 31 called Day. The identifiers Year, Month and Day are names of fields of TDateRec that behave as variables. However, the TDateRec type declaration doesn't allocate memory for the Year, Month and Day fields. The memory is allocated when you instantiate a record, which you do as follows:

The variable declaration creates two instances of TDateRec, called Record1 and Record2.

You can access to a record field by qualify the name of the field with the name of the record:

Or by using the  instruction:

Examples
The following example asks a question, retrieves the answer and prints the answer: means that the application waits for the user to enter a text and then saves the text in the variable.

This other example gets the number of a char in the ASCII: With _Input[1] you get the first char of _Input. And the first char is char 1, (not char 0 like in C++!)