Apex Data Types in Salesforce
Apex Data Types in Salesforce
Kajal Yadav
Data Types:
Apex Provide the Primitive Data Type to allow the Developer to work with simple values
in your code. Developers can use this data type to declare variables, perform Operation
and Manipulation data. Traditional Data types available across the programming
language .
1. Integer
2. Long
3. Double
4. Decimal
5. String
6. Date
7. Date and Time
8. ID
9. Boolean
10. Blob (Binary Large Objects)
Integer:
This datatype will store the Numerical values, which doesn't have decimal points inside it.
Note: It will store the values between the Range: -2^31 to +2^31 - 1. Each integer
variable will occupy 4 bytes of memory.
Note: Each Number field, Currency field, and percent field will reference the "Integer"
Datatype by default.
Ex:
Output:
Long:
This Datatype is used to store the Larger values. Which allows us to store
the values between the Range : -2^63 to +2^63 - 1. Each Long value will
Syntax :
Double:
Decimal:
This datatype is used to store Decimal values. Which occupies 8-bytes of memory to
store each value.
String:
This Datatype allows us to store alphanumerical values along with the special characters.
Note: Each Text field, Text Area, Text Area Long, Text Area Rich, Email,
Phone, URL, Text Encrypted, Picklist, Picklist-Multiselect, etc. fields will
reference the "string" datatype by default.
Syntax:
String <VariableName>=<Variable Value>;
Example:
This datatype is used to store "Date" values. Each Date field inside the object
will reference the Date Datatype by default.
Example:
DateTime:
ID:
This data type is used to store the 18-characters id value generated by salesforce.
Note: All ID fields, Lookup, Master-Details field types will reference the "ID datatype"
by default.
This datatype will store the TRUE / FALSE values inside it.
Note: Each Checkbox field will reference the "Boolean" datatype by default.
All the Attachments, Images, pictures will convert into Binary format and the
Binary format content will be storing inside the "Blob Datatype". Each Blob
Datatype variable can store the value of the size max. 2 GB
Note:
Collections store multiple values within a single variable. Apex supports three types:
Example:
Set collection supports the Dynamic memory allocation, Hence the memory size can
Grow / Shrink at runtime.
Key and Value can be either Primitive / SObject / Apex / Collection / User Defined
type.
Example:
✅ Use Specific Types → Prefer Integer, Decimal, Boolean over Object for better type
safety.
✅ Avoid Using String for Numbers → Use Integer or Decimal instead of String for
numeric values.
✅ Use Collections Wisely →