Strongly Typed
Learning Content
Last updated: July 21, 2026
Strongly Typed Language
One of the most important characteristics of Salesforce Apex is that it is a strongly typed programming language. Every variable, object, method parameter, and return value must have a predefined data type.
A data type tells the Apex compiler what kind of value a variable can store. This helps Salesforce detect programming errors during compilation instead of allowing incorrect values to cause unexpected behavior at runtime.
Whether you are storing a customer's name, an Opportunity amount, a record ID, or a collection of Accounts, Apex requires you to declare the appropriate data type before using the variable.
💡 LearnFrenzy Insight
Think of every variable as a labeled storage box.
If the label says Books, you should store only books inside it.
If the label says Shoes, you shouldn't put books inside that box.
Similarly, if a variable is declared as Integer, Salesforce allows only whole numbers to be stored in it.
Why is Apex a Strongly Typed Language?
Strong typing improves code quality, readability, and application stability. Since every variable has a defined type, Salesforce can validate your code before it is executed.
This prevents many common programming mistakes, such as assigning text to a numeric variable or performing invalid operations on incompatible data types.
| Benefit | Description |
|---|---|
| Better Code Quality | The compiler detects type-related errors before execution. |
| Improved Readability | Developers can immediately understand what kind of data a variable stores. |
| Safer Applications | Reduces unexpected runtime errors caused by invalid assignments. |
| Easy Maintenance | Clearly defined data types make applications easier to maintain. |
| Enterprise Reliability | Strong typing helps build scalable and secure Salesforce applications. |
Declaring Variables in Apex
The basic syntax for declaring a variable is:
DataType variableName = value;
Where:
- DataType specifies the type of value the variable can hold.
- variableName is the name of the variable.
- value is the data assigned to the variable.
Example 1: Integer Variable
The Integer data type stores whole numbers without decimal values.
// Execute Anonymous Window
Integer totalStudents = 150;
System.debug(totalStudents);
Output
150
Explanation
Integerstores whole numbers.150is assigned to the variabletotalStudents.System.debug()prints the value in the Debug Log.
🏢 Real Business Scenario
Suppose a company wants to count the number of active customers.
The total count can be stored in an Integer because customer counts are always whole numbers.
Example 2: Invalid Integer Assignment
An Integer variable cannot store text values.
Integer totalStudents = 'One Hundred';
Output
Compile Error:
Illegal assignment from String to Integer
⚠️ Common Mistake
Every variable must store only values that match its declared data type. Assigning a String to an Integer results in a compile-time error.
Example 3: Declaring Multiple Integer Variables
Integer boys = 45;
Integer girls = 55;
Integer total = boys + girls;
System.debug(total);
Output
100
Explanation
- The variables
boysandgirlsstore student counts. - The
+operator adds both values. - The result is stored in the variable
total.
Example 4: Using Integer in Salesforce Business Logic
The following example checks whether an Account has more than 100 Contacts.
Integer totalContacts = 125;
if(totalContacts > 100){
System.debug('Large Customer');
}
else{
System.debug('Regular Customer');
}
Output
Large Customer
✅ Best Practice
Choose meaningful variable names such as totalContacts, opportunityCount, or remainingBudget. Clear names make Apex code easier to understand and maintain.
Try It Yourself
Open the Developer Console, select Debug → Open Execute Anonymous Window, paste the following code, and click Execute.
Integer firstNumber = 25;
Integer secondNumber = 75;
Integer result = firstNumber + secondNumber;
System.debug('Total = ' + result);
Expected Output
Total = 100
🎯 Interview Tip
A frequently asked interview question is: "Why is Apex called a strongly typed language?"
A good answer is: Apex is strongly typed because every variable, method parameter, and return value must have a predefined data type. This allows the compiler to detect invalid assignments during compilation, improving code safety and reliability.
Quick Revision
- Apex is a strongly typed programming language.
- Every variable must be declared with a data type.
Integerstores whole numbers.- Invalid data type assignments produce compile-time errors.
- Meaningful variable names improve code readability.
- Strong typing helps build secure and maintainable Salesforce applications.
🚀 Next Part: Decimal, Double, Long, Boolean, String, Date, DateTime, and ID Data Types, with practical examples, expected outputs, and real-world Salesforce use cases.
Practice What You've Learned
Test your understanding with these practice exercises