Apex Data Types
What Are Data Types in Apex?
Data types in Apex define what kind of data you can store in a variable.
Think of a variable as a container or a box with a label on it. The label tells us what kind of thing we can put in the box.
Types of Apex Data Types (with Examples & Explanations)
1. Primitive Data Types
Data Type | What It Stores | Example | Precision / Notes |
---|---|---|---|
Integer | Whole numbers (no decimals) | Integer count = 100; // Valid Integer count = 10.5; // Invalid | ~±2.1 billion (32-bit) |
Long | Very large whole numbers | Long bigCount = 12345678900L; | ~±9.2 quintillion (64-bit) |
Double | Decimal numbers (floating-point) | Double price = 12.3456789; | ~15-16 digits precision |
Decimal | High-precision decimal numbers, used for financial calculations | Decimal total = 12345.67890123456789; | Up to 28 digits precision |
String | Sequence of characters (words, sentences) | String name = ‘Mubashira’; // Valid String city = “Mumbai”; // Valid | No fixed limit (max 6 million characters in some contexts) |
Boolean | true or false values only | Boolean isActive = true; Boolean isClosed = false; | 1 bit (true/false) |
Date | Stores a calendar date only | Date today = Date.today(); | Format: YYYY-MM-DD |
Time | Stores time (hours, minutes, seconds, milliseconds) | Time t = Time.newInstance(14, 30, 0, 0); | Format: HH:mm:ss.SSS |
Datetime | Stores both date and time | Datetime now = Datetime.now(); | Combines Date and Time |
ID | 18-character unique identifier of a Salesforce record | Id accId = ‘001xx000003DGXqAAO’; | Always 18 characters |
Blob | Binary data (images, files, etc.) | Blob fileData = Blob.valueOf(‘text’); | Used for file or binary content |
Object | Generic type to store any object instance | Object obj = ‘Hello’; | Use type casting to convert back |