Apex Language Fundamentals
Variables, Data Types & Operators
What Are Variables in Apex?
A variable is a container used to store data that your program can use or change.
Simple Explanation
Variables are like labeled boxes where you store information.
Common Data Types in Apex
Apex supports primitive data types similar to Java.
| Data Type | Example |
|---|---|
| Integer | 10 |
| Decimal | 99.99 |
| String | "Salesforce" |
| Boolean | true / false |
| Date | Date.today() |
Code Example
Integer age = 30; String name = 'Alex'; Boolean isActive = true; Decimal salary = 4500.75;
Operators in Apex
Operators are used to perform actions on variables.
| Operator Type | Example |
|---|---|
| Arithmetic | + - * / |
| Comparison | == != > < |
| Logical | `&& |
Example with Operators
if (age > 18 && isActive) {
System.debug('Eligible user');
}
Easy Rule to Remember
Data Type → Variable → Operator → Result
Gist (Quick Revision)
Variables store data, data types define what kind of data, and operators help perform calculations or comparisons.
Statements & Expressions
What Is a Statement?
A statement is a complete instruction that tells Apex to do something.
Example
age = age + 1;
What Is an Expression?
An expression produces a value.
Example
age + 1
Simple Difference
| Expression | Statement |
|---|---|
| Produces a value | Performs an action |
a + b |
a = a + b; |
Real-Life Example
Think of a calculator:
-
Expression →
2 + 3 -
Statement →
Display result = 5
Code Example
Integer total = price * quantity; System.debug(total);
Gist (Quick Revision)
Expressions calculate values; statements perform actions using those values.
3. Coding Conventions in Apex
Why Coding Conventions Matter
Good code is not just working code — it must be readable, maintainable, and professional.
Key Apex Coding Conventions
-
Use CamelCase for class names
-
Use camelCase for variables and methods
-
Meaningful names (avoid
a,x,temp) -
Proper indentation and spacing
Good vs Bad Example
❌ Bad Code
Integer a=10;
✅ Good Code
Integer totalAmount = 10;
Career Coach Tip
Interviewers often judge:
How you write code, not just whether it works.
Clean code shows experience and professionalism.
Common Naming Rules
| Element | Convention |
|---|---|
| Class | AccountService |
| Method | calculateTotal() |
| Variable | totalAmount |
| Constant | MAX_LIMIT |
Gist (Quick Revision)
Following coding conventions makes your Apex code easier to read, debug, and scale.
