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 the primitive types. Later chapters cover the numeric types, strings, and dates in detail.
What a primitive is
A primitive is a basic value type: a number, text, a true/false flag, or a point in time. Apex is strongly typed, so every variable declares its type before its name. Primitives are passed by value. If you assign one variable to another, you get an independent copy.
The primitive types
| Type | Holds | Notes |
|---|---|---|
| Integer | Whole number | 32-bit, about -2.1 billion to 2.1 billion |
| Long | Whole number | 64-bit, for values beyond Integer |
| Double | Floating-point number | 64-bit, approximate |
| Decimal | Number with a scale | Exact decimal arithmetic, used for currency fields |
| String | Text | Single quotes only |
| Boolean | true or false | Can also be null |
| Date | Calendar date | No time of day |
| Datetime | Date and time | |
| Time | Time of day | No date |
| Id | Salesforce record ID | 15 or 18 characters |
| Blob | Binary data | Files and encoded content |
Object is the parent of all types. It is not a primitive, but it appears later when you cast values.
Declaring one of each
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);The L suffix on 8000000000L marks the literal as a Long. Without it, Apex reads the literal as an Integer, and this value is too large for that type.
Choosing a type
The diagram shows the usual decision path for numbers, which is where most mistakes happen.
Salesforce Number and Currency fields map to Decimal, so Decimal is the safe default for record data.
Common errors
- Double for money. Doubles are approximate, so use Decimal when cents matter.
- Overflowing an Integer. Arithmetic that exceeds the 32-bit range overflows instead of switching to Long. Declare the variable as Long first.
- Double quotes around strings. Apex accepts only single quotes.
"Acme"is a compile error. - Assuming a default value. A variable you declare without a value holds null. It does not hold 0 or an empty string.
Exercise
You are storing a unit price of 19.99 and a world population of 8 billion. Which primitive type fits each value, and why?
Show answer
Use Decimal for the price, because currency needs exact decimal arithmetic. Use Long for the population, because 8 billion exceeds the Integer maximum of about 2.1 billion. The literal needs the L suffix, as in 8000000000L.