Introduction to Apex Flashcards
What is Apex?
Apex is Salesforce’s case-insensitive, cloud-hosted, Java-like, multitenant-aware, object-oriented, proprietary, strongly-typed, versioned programming language
What is a class?
a class is a template for objects that contains the methods (i.e. functions) that will define an object’s behavior and the variables that will hold an object’s state
What is an object?
an object is an instance of a class that has state (things that it knows about itself) and behavior (things that it can do)
What are the two types of type casting?
Explicit Type Casting
Implicit Type Casting
What is Explicit Type Casting?
Explicit Type Casting
- we convert between unrelated data types by invoking conversion methods
- many of the classes representing Apex variables have methods to convert values between data types
- we can also convert between related data types by putting the desired data type in parentheses after the assignment operator
String a = ‘1’;
Integer b = Integer.valueOf(a);
What is Implicit Type Casting?
Implicit Type Casting
- done automatically by language
- two types
casting between numeric primitive data types (hierarchy)
Integer can convert to => Long… => Double… => Decimal
casting between Ids and Strings
Integer a = 1;
Decimal b = a;
What are Lists?
- comma-separated, ordered, indexed groups of values (i.e. elements) that all share the same data type
- because Lists are ordered and indexed, we can access individual elements through their index in the List and the List can contain duplicate values
- Lists are zero-indexed,
Can you create a new list shoppingList and populate it with a “Cereal” and “Milk”?
List shoppingList = new List();
‘Cereal’,
‘Milk’
};
OR
List shoppingList = new List();
shoppingList.add(‘Cereal’);
shoppingList.add(‘Milk’);
What are sets? What methods does it have?
unordered collections and therefore must contain unique values
- add() - add element to set
- size() - returns size of set
- contains() - returns true/false
What is a Map?
groups of key-value pairs where each key must be unique, but we can have duplicate values
What do the following Control Flow do?
- Conditional Statements
- Switch Statements
- While and Do Loops
- For Loop
Conditional Statements
if ([Boolean_condition])
// Statement 1
else
// Statement 2
Switch Statements
switch on expression {
when value1 {
// code block 1
} when value2 {
code block 2
} when else { // default block, optional
// code block 4
}
}
While and Do Loops
while (condition) {
code_block
}
do {
code_block
} while (condition);
For Loop
Traditional
for (init; exit_condition; iterator) {
code_block
}
For Loop to iterate through Lists and Sets
for (variable : list_or_set) {
code_block
}