Data types are used to specify the different sizes and values that can be stored in a variable.
Apex supports the following data types −
- Primitive (Integer, Double, Long, Date, Datetime, String, ID, or Boolean)
- Enums
- sObject
- Classes and Interfaces
- Collections (Lists, Sets and Maps)
Apex Primitive Data Types
Here, we will discuss the Apex Primitive Data Types supported by Apex.
Boolean: This variable can either be false, true or null. In programming many times, this type of variable can be used as flag to identify if the particular condition is set or not set.
Apex Code Example:
Boolean b = false;
String: String is basically sequence of char values within single quotes. It does not have any limit for the number of characters. Here, the heap size will be used to determine the number of characters in Apex programming.
Apex Code Example:
String str = ‘Welcome to Salesforcedrillers.com’
Integer: It is a 32-bit number without any decimal value . The value range for this data type starts from -2,147,483,648 and the maximum value go up to 2,147,483,647.
Apex Code Example:
Integer i = 5;
Long: This is a 64-bit number without any decimal point. This is used when we need a range of values wider than integer.
Apex Code Example:
Long l = 50;
Decimal: A number that includes a decimal point. Decimal is an arbitrary precision number. In Apex Programming, Currency fields are automatically assigned the type Decimal.
And also Decimal has lot of built-in methods and rounding options.
Apex Code Example:
Decimal dec = 12.34
Double: This is a 64-bit number with a decimal point. This is used when we need a range of values wider than decimal.
Apex Code Example:
Double dbl = 5.1234
ID:A valid 18-alphanumeric character force.com record identifier.
Apex Code Example:
ID i = ‘b022w00000EKkeNBBO’
Note: If you set ID as 15-character value, Apex converts the value to its 18-character representation. All invalid ID values are rejected at runtime with runtime exception.
Blob: Blob is basically used to store files(e.g., images, emails, documents)
Date: This data 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 data type of DateTime.
Apex Code Example:
Date dt = date.today();
Datetime: This data type indicates a date with time. This can store the date with time.
Apex Code Example:
DataTime currentDT = DateTime.now();
Time: This data type indicates a time. This can store only the time.
Apex Code Example:
Time currentT = DateTime.now().time();
Note:
- Null is actually the absence of value so please do not be confused with an empty string or a zero which represents the actual values.
We can equate the Apex data types of field types in Salesforce as follows:
Salesforce Field Type | Apex Data Type |
Auto-Number | String |
Checkbox | Boolean |
Currency | By default assign as Decimal |
Date | Date |
Number | Integer or Double, depending on if you set the decimal place to zeroor more then zero when you create the field. |
Percentage | By default assign as Decimal |
Phone | String |
Text, Text(Encrypted), Text Area, Text Area(Long), Text Area(Rich) | String |
URL | String |
Enum
Enum is an abstract data type that is used to stores one value of a finite set of specified identifiers. You can use Enum keyword to define an Enum. Enum can be used as any other data type in Apex Programming Language.
Example of Enum :
public enum Season { SUMMER, WINTER, SPRING, FALL}
sObject
This is a special data type in Salesforce. It is very similar to a table in SQL and contains fields which are similar to columns in SQL. Sobjects are types of either standard or custom objects that stores record data in the force.com database.
For example, Account is a standard sObject and any other user-defined object (like Merchandise object that we created) is a Custom sObject.
Note: Developers refers to SObject and their fields by their API names into the program.
Apex Code Example:
// Declaring a sObject variable of type Standard Object – Account Account act = new Accont(); act.name = ‘SalesforceDrillers’; act.description = ‘Best site for learning new technologies’; System.debug(‘act variable value is = ’ + act); // Declaring a sObject variable of type Custom Object – Merchandise__c Merchandise__c mer = new Merchandise__c(); mer.name = ‘Jeans’; mer.description__c = ‘For Kids’; System.debug(‘mer variable value is = ’ + mer);
Apex keywords are also known as reserved words. Keywords are particular words which act as a key to a code. These are predefined words by Apex so it cannot be used as a variable or object name as per requirements.
- Class: This keyword is used to define/declare a class.
- Abstract: This keyword is used to define/declare a abstract class. An abstract class that contains abstract methods and non-abstract methods.
- Virtual: This keyword defines/declare a class or method that allows extension and overrides. We can’t override a method with the override keyword unless the class or method has been defined as virtual which means if a class is virtual then only that class allows extension and if method is virtual then allows overrides in sub class.
- Extends: Defines a class that extends another class, which means a child class can extends a parent class using extends keywords.
- Interface: Apex interface keyword is used to declare an interface. It can have only abstract methods. A class always implements an interface and an interface always extends another interface. Java implements keyword is used to implement an interface.
- Implements: Apex implements keyword is used to implement an interface.
- Static: This keyword defines a method/variable that is only initialized once at a class level, and it is associated with an (outer) class. We can call static variables/methods by class name directly. No need of creating instance of a class.
- Final: This keyword is used to define constants and methods that can’t be overridden within the child class.
- This: This keyword represents the current instance of a class , in constructor chaining.
- Super: This keyword invokes a constructor on a superclass.
- Return: This keyword returns a value from a method.
- Transient: This keyword declares instance variables that cannot be saved, and should not be transmitted as part of the view state in the visualforce controllers and extensions.
- Null: This keyword defines a null constant that can be assigned to any variable so that it removes the garbage value.
- Public: Apex public keyword is an access modifier. It is used to indicate that an item is accessible anywhere with the Salesforce environment.
- Global: Apex global keyword is an access modifier. It is used to indicate that the item is accessible anywhere. Which means within the Salesforce or outside the salesforce too, It has the widest scope among all other modifiers in Apex.
- With sharing & Without sharing keywords
With sharing Keyword: This keyword enforces the sharing rules of the org that apply to the current user. We usually called it user mode also.
Without sharing keyword: Ensures that the sharing rules of the org are not going to apply on current user. We usually called it system mode also.