Curriculum
Variables and Data Types are fundamental concepts in Apex programming. Every Salesforce application stores, processes, and manipulates data, and variables provide a way to hold that information temporarily during program execution. Data Types define the kind of information a variable can store, such as text, numbers, dates, or logical values.
Whether developing Apex Classes, Triggers, Batch Jobs, Integrations, or Lightning Controllers, developers constantly work with variables and data types. Understanding these concepts is essential before learning operators, control statements, collections, and object-oriented programming.
Mastering Variables and Data Types helps developers write efficient, readable, and maintainable Apex code.
A Variable is a named memory location used to store data temporarily during program execution.
Think of a variable as a container that holds information.
Examples:
Variables make it possible to process and manipulate information dynamically.
Variables allow developers to:
Without variables, programming would not be possible.
Store values temporarily.
Use values multiple times.
Make code easier to understand.
Allow dynamic behavior.
Simplify application updates.
Variables are one of the most important programming concepts.
Before using a variable, it must be declared.
Syntax:
DataType variableName;
Example:
String studentName;
This declares a variable named studentName.
Initialization assigns a value to a variable.
Example:
String studentName = 'Rahul Sharma';
Here:
The variable is now ready for use.
Variables can be assigned values after declaration.
Example:
String courseName;
courseName = 'Salesforce Development';
Assignment stores data inside the variable.
A Data Type defines the type of information a variable can store.
Examples:
Using appropriate data types improves performance and accuracy.
Apex data types are broadly categorized into:
Basic values.
Complex structures.
Both are important in Salesforce development.
Primitive Data Types store single values.
Examples:
These are the most commonly used data types.
Integer stores whole numbers.
Example:
Integer totalStudents = 500;
Valid Values:
10
50
1000
50000
Integers do not store decimal values.
Integer age = 25;
System.debug(age);
Output:
25
Integers are commonly used for counts and quantities.
Decimal stores numbers containing decimal points.
Example:
Decimal courseFee = 14999.99;
Decimals are commonly used for financial calculations.
Decimal revenue = 250000.75;
System.debug(revenue);
Output:
250000.75
Decimals provide higher precision than integers.
Double stores large decimal values.
Example:
Double percentage = 98.75;
Double supports scientific and mathematical calculations.
Long stores very large whole numbers.
Example:
Long population = 1500000000;
Long supports larger values than Integer.
String stores text values.
Examples:
Strings are among the most commonly used data types.
String studentName = 'Rahul Sharma';
System.debug(studentName);
Output:
Rahul Sharma
Strings store character-based information.
Example:
String firstName = 'Rahul';
String lastName = 'Sharma';
String fullName = firstName + ' ' + lastName;
Output:
Rahul Sharma
This process is called concatenation.
Boolean stores only two values:
true
false
Used for decision-making.
Boolean isActive = true;
System.debug(isActive);
Output:
true
Boolean variables support logical operations.
Date stores calendar dates.
Example:
Date enrollmentDate = Date.today();
Date values contain:
Dates are frequently used in Salesforce applications.
Date currentDate = Date.today();
System.debug(currentDate);
Output:
2026-06-01
Date values support scheduling and reporting.
Datetime stores both date and time information.
Example:
Datetime currentTime = Datetime.now();
Useful for tracking events and activities.
Datetime loginTime = Datetime.now();
System.debug(loginTime);
Output:
2026-06-01 10:30:45
Exact output depends on execution time.
Time stores only time values.
Example:
Time classTime = Time.newInstance(10, 30, 0, 0);
Useful for scheduling operations.
ID stores Salesforce record identifiers.
Example:
Id accountId = '001XXXXXXXXXXXX';
Every Salesforce record has a unique ID.
Account acc = [
SELECT Id
FROM Account
LIMIT 1
];
Id accountId = acc.Id;
IDs are frequently used in Apex development.
Null indicates that a variable has no value assigned.
Example:
String courseName = null;
Developers should handle null values carefully.
String studentName = null;
if(studentName == null){
System.debug('No Value Found');
}
Output:
No Value Found
Null checking prevents runtime errors.
Variables should follow naming standards.
Valid Examples:
studentName
courseFee
totalStudents
isActive
Invalid Examples:
1student
student-name
class
Proper naming improves readability.
Good:
studentName
Bad:
s
Example:
studentEnrollmentDate
Use clear names whenever possible.
These practices improve code quality.
A constant stores a value that should not change.
Example:
final Decimal TAX_RATE = 18.0;
Constants improve maintainability.
Type Conversion changes one data type into another.
Example:
Integer totalStudents = 100;
String studentCount =
String.valueOf(totalStudents);
Output:
100
Type conversion is common in Apex development.
Scope determines where a variable can be accessed.
Types:
Inside methods.
Inside classes.
Shared across instances.
Scope controls visibility.
public static void displayMessage(){
String message = 'Welcome';
System.debug(message);
}
The variable exists only within the method.
Developers should avoid these mistakes.
A software training company stores:
String studentName = 'Rahul Sharma';
String courseName =
'Salesforce Development';
Decimal courseFee = 14999.99;
Boolean feePaid = true;
These variables help manage student information efficiently.
This demonstrates how variables and data types are used in business applications.
Understanding Variables and Data Types helps professionals:
Variables are the foundation of all Apex programming.
Variables are named memory locations used to store data during program execution, while Data Types define the type of information that variables can hold. Apex supports numerous data types including Integer, Decimal, String, Boolean, Date, Datetime, Time, and ID. Understanding Variables and Data Types is essential for building Apex applications, processing Salesforce data, and creating scalable business solutions.
A variable is a named storage location used to hold data during program execution.
A data type defines the kind of information a variable can store.
The String data type stores text values.
The Boolean data type stores true and false values.
Date stores only the date, while Datetime stores both date and time.
Variables allow developers to store, process, and manipulate data efficiently.
Looking to learn more technologies and programming skills?
WhatsApp us