Variables, Constants and Data Types
4.1 Declaring Variables
Global variables are zero-initialized. Local variables of unmanaged types (Integer, Double, pointers, records without managed fields) are not initialized. Managed types (string, dynamic arrays, interfaces, variants) are always initialized to empty/nil.
var
Count: Integer;
Price: Double;
Name: string;
IsActive: Boolean;
A, B, C: Integer;
Total: Currency = 0; // initialized global variable (globals only)
begin
Count := 10; // assignment uses :=
// Inline variables (Delphi 10.3+): declared where they're used, with type inference
var Message := 'Hello'; // inferred as string
var I: Integer := 5;
for var J := 1 to 10 do
Writeln(J);
end;4.2 Constants
const
MaxUsers = 100; // true constant (type inferred)
Pi2: Double = 6.283185307; // typed constant
AppName = 'Inventory';
Weekdays: array[0..6] of string = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
Origin: TPoint2D = (X: 0; Y: 0); // record constant
resourcestring
SErrNotFound = 'Item %s was not found'; // localizable string resource4.3 Data Types
ShortInt (8), SmallInt (16), Integer/LongInt (32), Int64 (64), Byte (8u), Word (16u), Cardinal/LongWord (32u), UInt64 (64u), NativeInt/NativeUInt (pointer-size)
Integer is always 32-bit. NativeInt is 32/64 by platform.
Single (4), Double (8), Extended (10 bytes on Win32, 8 on Win64/others), Real (= Double), Currency (8, fixed 4 decimals), Comp Use Currency for money
Boolean, ByteBool, WordBool, LongBool Char (= WideChar, UTF-16), AnsiChar Unicode since 2009
string (= UnicodeString), AnsiString, UTF8String, RawByteString, ShortString, WideString (COM BSTR)
string is reference counted, copy-on-write Pointer, PChar, PInteger, typed ^T Variant, OleVariant arrays, records, sets, files, classes, interfaces
procedure, function, of object, reference to
4.4 Type Declarations
type
TAge = 0..150; // subrange
TColorKind = (ckRed, ckGreen, ckBlue); // enumeration
TScores = array of Integer; // dynamic array type
TPersonName = type string; // distinct type
PInteger = ^Integer; // pointer type
TCompare = function(A, B: Integer): Integer;4.5 Type Information and Limits
Writeln(SizeOf(Integer), ' ', SizeOf(Int64), ' ', SizeOf(Extended));
Writeln(High(Integer), ' ', Low(Integer), ' ', High(Byte));
Writeln(MaxInt, ' ', MaxLongint);