Learn Apex - 10 Code Examples & CST Typing Practice Test
Apex is a strongly typed, object-oriented programming language developed by Salesforce for building scalable, secure, and automated applications on the Salesforce platform. It allows developers to execute business logic on the server side, integrate external systems, and customize CRM workflows using a syntax similar to Java.
Learn APEX with Real Code Examples
Updated Nov 19, 2025
Code Sample Descriptions
Apex Counter and Theme Toggle
public class Counter {
public Integer count = 0;
public Boolean isDark = false;
public void updateUI() {
System.debug('Counter: ' + count);
System.debug('Theme: ' + (isDark ? 'Dark' : 'Light'));
}
public void increment() { count++; updateUI(); }
public void decrement() { count--; updateUI(); }
public void reset() { count = 0; updateUI(); }
public void toggleTheme() { isDark = !isDark; updateUI(); }
}
// Simulate actions
Counter c = new Counter();
c.updateUI();
c.increment();
c.increment();
c.toggleTheme();
c.decrement();
c.reset();
Demonstrates a simple counter with theme toggling using Apex classes and console output (system debug).
Apex Simple Addition
public class AddProgram {
public Integer a = 5;
public Integer b = 3;
public Integer sum;
public void calculate() {
sum = a + b;
System.debug('Sum: ' + sum);
}
}
AddProgram ap = new AddProgram();
ap.calculate();
Adds two numbers and prints the result using System.debug.
Apex Factorial
public class FactorialProgram {
public Integer n = 5;
public Integer fact = 1;
public void calculate() {
for(Integer i = 1; i <= n; i++) {
fact *= i;
}
System.debug('Factorial: ' + fact);
}
}
FactorialProgram fp = new FactorialProgram();
fp.calculate();
Calculates factorial of a number using a loop.
Apex Fibonacci Sequence
public class FibonacciProgram {
public void generate() {
List<Integer> fib = new List<Integer>{0,1};
for(Integer i=2;i<10;i++) {
fib.add(fib[i-1]+fib[i-2]);
}
System.debug('Fibonacci: ' + fib);
}
}
FibonacciProgram fp = new FibonacciProgram();
fp.generate();
Generates first 10 Fibonacci numbers using Apex lists.
Apex Max of Two Numbers
public class MaxProgram {
public Integer a = 7;
public Integer b = 10;
public void findMax() {
Integer max = (a > b) ? a : b;
System.debug('Max: ' + max);
}
}
MaxProgram mp = new MaxProgram();
mp.findMax();
Finds the maximum of two numbers.
Apex Array Sum
public class ArraySum {
public void sumArray() {
List<Integer> nums = new List<Integer>{1,2,3,4,5};
Integer sum = 0;
for(Integer n : nums) { sum += n; }
System.debug('Sum: ' + sum);
}
}
ArraySum as = new ArraySum();
as.sumArray();
Sums elements of an integer list.
Apex Even Numbers Filter
public class EvenNumbers {
public void filter() {
List<Integer> nums = new List<Integer>{1,2,3,4,5,6,7,8,9,10};
for(Integer n : nums) {
if(n % 2 == 0) System.debug(n);
}
}
}
EvenNumbers en = new EvenNumbers();
en.filter();
Displays even numbers from a list.
Apex String Concatenation
public class ConcatStrings {
public void concat() {
String str1 = 'HELLO';
String str2 = 'WORLD';
String result = str1 + str2;
System.debug('Concatenated: ' + result);
}
}
ConcatStrings cs = new ConcatStrings();
cs.concat();
Concatenates two strings and prints the result.
Apex Counter With Loop Simulation
public class LoopCounter {
public void run() {
Integer count = 1;
while(count <= 5) {
System.debug('Counter: ' + count);
count++;
}
}
}
LoopCounter lc = new LoopCounter();
lc.run();
Counts from 1 to 5 using a loop with debug output.
Apex Conditional Increment
public class ConditionalIncrement {
public void run() {
Integer count = 3;
if(count < 5) count++;
System.debug('Counter: ' + count);
}
}
ConditionalIncrement ci = new ConditionalIncrement();
ci.run();
Increment counter only if below 5.
Frequently Asked Questions about Apex
What is Apex?
Apex is a strongly typed, object-oriented programming language developed by Salesforce for building scalable, secure, and automated applications on the Salesforce platform. It allows developers to execute business logic on the server side, integrate external systems, and customize CRM workflows using a syntax similar to Java.
What are the primary use cases for Apex?
Salesforce triggers & automation. Custom REST & SOAP APIs. Batch & scheduled jobs. Complex CRM business logic. Integrations with external systems. Custom Salesforce Apps & packages
What are the strengths of Apex?
Native integration with Salesforce objects. Secure, scalable execution. Excellent tooling and test framework. Great for enterprise automation. Powerful asynchronous capabilities
What are the limitations of Apex?
Vendor lock-in to Salesforce ecosystem. Strict governor limits. Cannot interact with system file I/O. Restricted multithreading. Must follow Salesforce deployment rules
How can I practice Apex typing speed?
CodeSpeedTest offers 10+ real Apex code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.