Sub topic 2
Primitive data types at a glance
This chapter assumes you know what a variable and an expression are, and that you can run anonymous Apex in the Developer Console or an IDE. It gives you a map of Apex's primitive types. Later chapters cover the numeric types, strings and dates in depth.
What counts as a primitive
A primitive type holds a single simple value, not a collection of values or a record. Apex is strongly typed, so every variable declares its type before its name, and the compiler rejects assignments that do not fit.
Apex has these primitive types:
| Type | Holds | Notes |
|---|---|---|
| `Integer` | 32-bit whole number | Range -2,147,483,648 to 2,147,483,647 |
| `Long` | 64-bit whole number | Literals need an `L` suffix when they exceed the Integer range |
| `Double` | 64-bit floating-point number | Fast, but inexact for decimal fractions |
| `Decimal` | Arbitrary-precision number with a scale | Use for money and exact values |
| `String` | Text | Wrapped in single quotes |
| `Boolean` | `true`, `false`, or `null` | Three possible states, not two |
| `Date` | A calendar date | No time or time zone |
| `Datetime` | A date and a time | Includes a time zone context |
| `Time` | A time of day | No date |
| `Id` | A Salesforce record ID | 15- or 18-character form |
| `Blob` | Binary data | Files, encoded content |
| `Object` | Any value | The base type that everything can be assigned to |
Apex has no char or float type. A single character is a one-character String.
Apex passes primitives by value. If you pass an Integer to a method and the method changes its parameter, your original variable is unchanged.
Declaring one of each
Each declaration names the type, the variable, and an optional starting value.
Integer count = 5;
Long population = 8000000000L;
Double ratio = 0.5;
Decimal price = 19.99;
String name = 'Acme';
Boolean isActive = true;
Date today = Date.newInstance(2026, 11, 30);
System.debug(name + ' ' + count);8000000000L is outside the Integer range. Without the L, the compiler treats the literal as an Integer and reports an error. The L tells it to read the literal as a Long.
Date values are not written as literals. You build them with factory methods such as Date.newInstance(year, month, day).
Choosing a type
The diagram shows the questions to ask first.
A few rules of thumb:
- Default to
Integerfor counts and loop indexes. - Use
Longfor values such as milliseconds since an epoch, which exceed 32 bits quickly. - Use
Decimalfor currency and anything you will round or compare exactly. - Use
Doublemainly when a platform method or a field requires it.
Double versus Decimal
Double stores a binary approximation, so some decimal fractions cannot be represented exactly. Decimal stores decimal digits and a scale, which avoids that problem.
Double a = 0.1;
Double b = 0.2;
System.debug(a + b == 0.3);
Decimal total = 10.00;
Decimal rate = 0.075;
Decimal tax = (total * rate).setScale(2, RoundingMode.HALF_UP);
System.debug(tax);The first comparison prints false, because the Double sum is slightly more than 0.3. The Decimal calculation gives 0.75, and setScale(2, RoundingMode.HALF_UP) fixes the number of decimal places and states how to round. Salesforce currency fields map to Decimal, so using Decimal in your code avoids conversions.
Default values and null
A variable you declare without a value is null. It is not zero, an empty string, or false.
String note;
System.debug(note == null);
Integer total = null;
System.debug(total == null);Both lines print true. Null is a legal value for every primitive type, including Boolean, and it causes the most common primitive-type errors:
- Calling a method on a null
String, such asnote.length(), throws aSystem.NullPointerException. - Using a null
Booleanas a condition, as inif (flag), also throws. - Arithmetic on a null
IntegerorDecimalthrows.
Check for null before you use a value you did not create yourself, such as a field read from a record or a method argument.
Id, Blob, and Object
Id holds a Salesforce record ID. If you assign a valid 15-character ID, Apex converts it to the 18-character form. An invalid string assigned to an Id variable throws a runtime error, so the type also acts as a validity check. The first three characters of an ID identify the object type.
Blob holds raw binary data, for example an attachment body or the result of a cryptographic call. You rarely do arithmetic or comparison on a Blob. You convert it, often with EncodingUtil.base64Encode or Blob.toString().
Object can hold a value of any type. You must cast it back to a specific type before using type-specific methods. Casting is covered in a later chapter.
Case-insensitive strings
Apex strings compare differently from most languages. The == operator ignores case, while equals is case-sensitive.
String a = 'apex';
String b = 'APEX';
System.debug(a == b);
System.debug(a.equals(b));
System.debug(a.equalsIgnoreCase(b));This prints true, false, true. Use equals when case matters, for example when comparing tokens or case-sensitive codes.
Common mistakes
- Assigning a large literal to a
Longwithout theLsuffix. - Using
Doublefor money and getting rounding surprises. - Assuming an uninitialized
Booleanisfalse. - Assuming
==on strings is case-sensitive, as it is in Java. - Calling a method on a variable that might be null without a check.
Exercise
You declare Boolean isReady; and then run if (isReady) { System.debug('go'); }. What happens, and what are two ways to fix it?
Show answer
The variable is null because it was never initialized, so using it as the if condition throws a System.NullPointerException. Fix it by initializing it (Boolean isReady = false;) or by comparing explicitly (if (isReady == true)), which treats null as not true.
Exercise
Which type would you choose to store 8,000,000,000, and what must the literal look like?
Show answer
Use Long, and write the literal with an L suffix: Long population = 8000000000L;. The value exceeds the Integer maximum of 2,147,483,647.
Exercise
Given String a = 'apex'; String b = 'APEX';, what do a == b and a.equals(b) return?
Show answer
a == b returns true because Apex string equality with == ignores case. a.equals(b) returns false because equals is case-sensitive.