
Salesforce Apex Tutorial – Chapter 4: Data Types In Apex
Apex is a strongly typed language, every variable in the apex must be declared with a specific data type. All apex variables were initially assigned a null value.
You can read the previous article Salesforce Apex Tutorial – Chapter 3: Apex Examples and next article Salesforce Apex Tutorial – Chapter 5: Apex Variables.
Here is a list of data types supported by Apex:
- Primitive (Integer, Double, Long, Date, DateTime, String, ID, Blob, and Boolean).
- Collections (Lists, Sets, and Maps).
- sObject
- Enums
- Classes, Objects, and Interfaces
Let’s discuss the Primitive data types, sObject, and enums in detail.
Primitive Data Types:
- Integer:
It is a 32 bit number that does not contain any decimal points. Its value ranges between -2,147,483,648 and 2,147,483,647. Example:
Integer quantity = 100;System.debug(‘The value of the quantity variable is: ’+quantity); |
The system.debug statement prints the value of the quantity variable.
- Boolean:
A boolean variable can only be assigned a true, false, or null value. A boolean variable is used in control statements to determine the flow of a program. Example:
Boolean isCheck= true; |
In the above statement, we have declared the isCheck variable and initialized it to true.
- Date:
A variable of date datatype is used to store a date. A date variable doesn’t have any information about time. A DateTime variable can be used to store time along with the date. Example:
Date deliveryDate= system.today(); |
- Long:
It is a 64-bit number that does not contain any decimal points. This data type should be used to assign a value wider than the range supported by the integer. Example:
Long turnOver= 21474838973344648L; |
- Enum:
An enum type is a special data type that enables a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.
Example:
Declaring an Enum for compass directions.
public enum CompassDirection{North, South, East,West}CompassDirection obj = CompassDirection.East; |
- sObject:
It is a special data type in the apex. A sObject variable represents a row of data and can only be declared in Apex using the SOAP API name of the object.
Example:
//Declaring a sObject variable of type contact Contact contact = new Contact(); //Assignment of values to the contact fields contact.FirstName = ‘Test’;contact.LastName = ‘Name’; |
Comments are closed.