Experience Salesforce
Apex Variables and Apex Literals in Salesforce
What You’ll Learn
- What are Apex Variables in Salesforce?
- Types of Variables in Apex
- What are Apex Constants in Salesforce?
- What are Apex Literals in Salesforce?
A variable is a named value holder in memory. Being the foundational element in programming, variables let you store, alter, and retrieve data while your program executes, giving it flexibility and dynamic behavior. Variables are used to store data that can be changed or manipulated within the program. Understanding how to use Apex variables in Salesforce is fundamental.
Let’s explore more about look at the types of variables in Apex
What are Apex Variables in Salesforce?
A variable in Apex is a designated storage place that contains an adjustable value while the program is running. This data may take the shape of a text message, number, date, or even a complicated piece of information like a customer record. When defining a variable in Apex, you first give it a name, indicate its type (such as Date, String, or Integer), and optionally set its value. In Apex, local variables are declared using Java-like syntax.
Apex variables in Salesforce can be defined at any point in the code block and are typically case-insensitive. They initialized to null if no other value is assigned to the apex variable during declaration.
The name we choose for a variable is called an identifier. Identifiers can be of any length, but they must begin with an alphabet. The rest of the identifiers can also include digits.
- Operators and spaces are not allowed.
- We can’t use any of the apex reserved keywords when naming variables, methods, or classes.
- Keywords are the words that are essential for the Apex language.
Integer i_a; 🗸 | Integer i 2; ☓ |
Integer _ia; ☓ | Integer i$2; ☓ |
Integer ia_; ☓ | Integer $i2; ☓ |
Integer i2; 🗸 | Integer $_i_2; ☓ |
Types of Variables in Apex
Types of Variables in Apex
1. Primitive Data Types
These are the basic data types used to store simple values.
Examples:
Integer: Whole numbers (e.g., -10, 0, 12345)
Long: Larger whole numbers (e.g., for very large account IDs)
Double: Decimal numbers (e.g., currency values, calculations)
Decimal: High-precision decimal numbers (e.g., financial calculations)
String: Text data (e.g., customer names, addresses)
Date: Represents a specific date (e.g., order date)
DateTime: Represents a particular date and time (e.g., meeting schedule)
Boolean: True or False values (e.g., indicating active/inactive status)
2. sObject Types
These represent the core objects within the Salesforce platform (like accounts, contacts, and opportunities). An sObject variable can hold information about a single record from a specific Salesforce object.
Example:
Account myAccount = new Account(); This creates a sObject variable named myAccount that can hold data for a single Account record.
Read More:
sObjects in Salesforce
3. Collections
Collections allow you to store and manage multiple values of the same or different data types.
Common types:
List: An ordered collection of items, allowing duplicates (e.g., a list of product IDs for an order).
Set: Unordered collection of unique items (e.g., set of exceptional customer IDs).
Map: Key-value pairs, where each key uniquely identifies a specific value (e.g., map of product IDs to their corresponding product names).
Read More:
Collections in APEX
4. Enums
Enums define a collection of named constants, limiting a variable to a specific set of predefined options. They make code more readable and prevent typos or errors associated with using hardcoded values. These are a distinct type that consists of a fixed set of constants.
Read More:
Enums in APEX
What are Apex Constants in Salesforce?
Apex Constants in Salesforce are the variables whose values don’t change after being initialized once. Constants can be defined using the final keyword. Constants can be assigned almost once, either in the declaration itself or with a static initialization method if the constant is described in a class.
Note:
We cannot use the final keyword in the declaration of a class or a method because, in apex, classes and methods are final by default and cannot be overridden or inherited without using the virtual keyword.
What are Apex Literals in Salesforce?
Like variables in other programming languages, apex literals are the fundamental building blocks of data in Apex programs. They stand for set values that are immutable while a program is running. Understanding Apex literals is essential for writing any Apex code, as they provide the fundamental data elements you work within your program.
Types of Apex literals in Salesforce
- Numbers: Integers (whole numbers) and decimals. (e.g., 123, 3.14)
- Strings: Text enclosed in single quotes (“). (e.g., ‘Hello world!’)
- Booleans: True or False values. (e.g., true, false)
- Characters: Single characters enclosed in single quotes. (e.g., ‘a’, ‘Z’)
- Dates and Times: Represent specific points in time. (e.g., date {2024-05-20})
Need Extra Support? Our FREE study materials have got you covered.
Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.