Apex DataTypes
Apex DataTypes
upon which everything else is build. A Variable is a store for some data that can
change (or vary) over time.
Apex DataTypes
==============
1. Primitive Data Type - Integer, Double, Long, Date, Datetime, String, ID, or
Boolean)
2. Non Primitive Data Type - Collections (List, Set, Map)
3. SObject - Account, Contact, Opportunity, Lead, Task, User,..... (Any Salesforce
Obejct - Standard/Custom)
Syntax:
DataType VariableName = value;
1. Integer -
The Integer datatype can hold any whole number between 2,147,483,647 and -
2,147,483,648.
A 32-bit number that does not include any decimal point.
2. Long -
A Long values is a very big Integer, which is a value without a decimal point
between 2^63 - 1 and 2^63.
3. Decimal
The Decimal data type is used for storing numerical data including decimals but
that have a fixed scale that can either be set explicitly or from the data creating
the variable.
4. Double
Similar to Decimal is Double, again a number involving a decimal point, but in this
instance a 64-bit number with a maximum value of 2^63 - 1 and a minimum value of
2^63.
5. Boolean -
Boolean values are simple true or false values used for logical processing or
holding some state which is either on or off.
This variable can either be true, false or null. Many times, this type of variable
can be used as flag in programming to identify if the particular condition is set
or not set.
6. Date
The Date data type stores information on a particular day without holding any
information on the time of day.
This variable type indicates a date. This can only store the date and not the time.
For saving the date along with time, we will need to store it in variable of
DateTime.
Eg: 1
Date christmas = Date.newInstance(2023, 12, 25);
Eg : 2
//ShipmentDate can be stored when shipment is dispatched.
Date ShipmentDate = date.today();
System.debug('ShipmentDate '+ShipmentDate);
7. DateTime
Datetime is a primitive holding both a date and a particular time on that date,
commonly used for either time-dependent information such as appointment time, or
for storing auditing and logging information such as the day and time on which an
action occured.
Eg: 1
Eg: 2
Datetime meetingStart = Datetime.newInstance(2023, 11, 6, 7, 30, 0);
8. Time
Time is very similar to Date and DateTime and represents a timeStamp that is not
dependent on a day.
Eg : 2
9. String
The String datatype is for any collection of characters enclosed within single
quotation marks such as:
In the final variable, we declare a variable called greeting. You can see we are
able to concatenate sgtrings together using the + symbol so that greeting has the
value Hello Adnan
Other Examples
Output:
My
carriage return string
Output:
My
line feed string
Output:
My 'single quote string'
Output:
My "double-quote string"
Output:
My\blackslash string
You can use the following escape sequences for special characters in string values.
SEQUENCE MEANING
\b One backspace character
\n New line
\r Carriage return
\t Tab
\Z CTRL+Z (ASCII 26)
\” One double-quote character
\\ One backslash character
\0 One ASCII null character
10. Id
This is a special data type available to Apex and Salesforce programmers, and is
the 18 character Id for a particular record. Salesforce verifies the Id at runtime
when a variable of type Id is assigned a value and will error if the value is
invalid. It will also convert a 15 character Id into an 18 character Id as part of
this Process;
ID accountId = '0012w00001R7LpQAAV';
11. Blob
A blob is a set of binary data stored in a single object. Blob values are commonly
used in processes where you are either encrypting to a Blob or decrypting from a
Blob to add some additional security to a set of data.
Eg:
Object data = 'Adnan';
System.debug(data);
Object dt = date.today();
System.debug(dt);