Primitive Data Types in Apex
In this blog post we will learn about the different primitive data types which are available in Apex. Primitive data types are basic pieces of a programming language that include built-in support. In most languages, these types cannot be modified and are used as basic building blocks to construct application logic. The primitive data types in Apex are as follows:
Blob – a collection of binary data. Typically used to represent the data of files. Can be converted to String (see String) and vice versa as needed.
Boolean – variables of this type can only be assigned as true, false, or null (see Null).
Date – represents a date and contains no time data.
Datetime – represents a specific point in time . For example, timestamps. Contains information about both date and time.
Note: Datetime values are stored in the force.com database in UTC time. However, when querying this data and displaying on a Visualforce page using the outputField component, the value is calculated based on the current user’s timezone setting on their profile.
Decimal – A number with a decimal point. The default type for Currency fields. Decimal variables have an arbitrary precision (number of digits after the decimal point) which is set when the decimal value is first assigned, unless explicitly set.
Double – A very large number which can include a decimal, with a minimum value of -263 and maximum value of 263-1. Should only be used for very large numbers and complex calculations. See also Long.
ID – Any valid 18-character Force.com record ID
Note: If you set a variable of type ID to a 15-character value, Apex will convert it to the corresponding 18-character ID automatically. Be careful though, this will not work if you set a variable of type string to a 15-character ID, Apex will leave that alone.
Integer – A whole number , meaning there is no decimal point, which can range in value from -2,147,483,648 through 2,147,483,647.
Long – Similar to double, but does not contain a decimal point ; it’s the same as an integer, but can be used to represent much larger numbers than an integer. Use this if you need to represent a number outside the range of integers only. Long values can range from -263 through 263-1 (that is a really big number!).
Object – This is a special variable type which can be used to represent any other type, including user-defined objects (we will see this later). However, before actually performing any action on an Object variable, it must be converted (cast) to a specific type.
String – Any set of characters, such as a word, surrounded by quotes. String is arguably the most versatile type (other than Object) as it can be cast to most other types, and is easy to store and manipulate. Strings have no limit on a maximum length, but the Heap Size Limit is used to control the program’s size. The Heap Size Governor Limit will be covered in the Governor Limits section of this chapter.
Note An empty string (no characters) is not the same as a null string. It is perfectly acceptable to assign the value of ” (empty string, not even a space) to a string variable—this is simply an empty value.
Time – represents a particular time . Can only be used with system methods (very limited use cases).
Null – Although null is technically not a primitive type per se, it is a special constant value that is used to represent “nothing”. All variables declared, but not assigned a value, have the value of null assigned automatically.
Note: A very common mistake by new developers, and even veterans, is to assume that a boolean variable is assigned the value of false if no value is set. In Apex, all variables, including booleans are initialized as null until a value is assigned.