What is Apex ?- Definition from Trenovision

Apex

Apex is a strongly-typed programming language that executes on the Force.com Platform. Apex is used to add business logic to applications, to program controllers in the user interface layer.
 


Apex data types

Apex supports a number of different data types:

  • Primitive data types (Integer, Date, String, Boolean…)
  • Enums (an enumerated list)
  • sObject types that represent persistent objects
  • collections (list, set, map of primitives)
  • An object created from user or system-defined Apex classes
  • Null (for the null constant)



Primitive Data Types

The primitive data types include:

  • Blob – for storing binary data
  • Boolean
  • Date, Time and Datetime
  • Decimal – for representing arbitrary precession numbers, including currency
  • ID – the Force.com database record identifier type
  • Integer, Long, Double and String
  • Object – can be used as the base type for any other type; supports casting

Example
Here are some variable definitions for primitives:
DateTime dt = System.now() + 1;
Boolean mustI = true;
String s = ‘abc’.toUpperCase();
Decimal d = Decimal.valueOf(‘123’);
 


Enum Data Type

  • Enum(or Enumerated list) is an abstract data type that stores one value of a finite set of specified identifiers.
  • To define an Enum, use the enum keyword in the variable declaration and then define the list of values.
  • For example:

public enum Season {WINTER, SPRING, SUMMER, FALL}
Season e = Season.WINTER;
Season m(Integer x, Season e) {
If (e == Season.SUMMER) return e; //…
}

  • By creating this enum, you have created a new data type called season that can be used as any other data type.



Collection Data Types

Collection data types store group of elements of primitive, composite, or collection data types.
There are three different types of collections in Apex:

  • List: an ordered collection of primitives or composite data types distinguished by its index.
  • Set: an unordered collection of unique primitives or sObjects.
  • Map: an unordered collection of unique, primitive keys that map to single values, which can be primitives or composite data types.



List Data Type

  • Each element consists of two pieces of information
    • An index: an integer
    • A value: the data
  • The index of the first element is zero.

example:
List<DataType> listname=new List<DataType>();
List<string> strlist=new List<String>();
String[] strlist= new string[] ; (List data type: array Notation)
 


List Methods

  • add/remove: adds or removes elements at a given index or at the end of the list.
  • clear: removes all the elements from the list.
  • clone/deepClone: creates a copy of the list either by cloning references or the values.
  • get/set: retrives or assigns values attached to a certain index.
  • isEmpty: returns true if the list has zero elements.
  • size: returns the number of elements in the list.
  • sort: sorts list of primitives in the ascending order.

Set data type

There are built in methods for sets that include:

  • Add or remove elements to the set.
  • Verify if set contains certain elements.
  • Size the set

Examples:
Set<DataType> setname= new set<DataType>();
Set<integer> setint= new Set,integer>();
setint.add(25);
setint.add(7);
setint.size();
setint.remove(7);
 


Set methods

  • add/addAll: add element(s) to the set if they are not already present.
  • clear: removes all the elements from the set.
  • clone: creates a copy of the set.
  • Contains/containsAll: returns true if the set contains all elements specified.
  • isEmpty: returns true if the set has zero elements.
  • size: returns the number of elements in the set.
  • Remove/removeAll: removes all the elements from the set.
  • retainAll: retains only the elements specified and removes the rest.

Map Data Type

  • Maps often used to map IDs to sObjects.
    • Map<PrimitiveKeyDataType, DataType> mapName= new
    • Map<PrimitiveKeyDataType, DataType>();
  • There are built in methods that you can do with maps, which include
    • Put or remove elements from the map.
    • Get values for a particular key.
    • Verify if the map contains the key.

Examples:
Map<String, String> keymap=new Map<string, String>();
keymap.put(‘white’, ‘house’);
keymap.put(‘purple’, ‘garage’);
keymap.get(‘white’);
keymap.containskey(‘green’);
 


Map Methods

  • clear: removes all key value pairs from the map.
  • clone/deepClone: creates a copy of the map either by cloning references or the values.
  • ContainsKey: returns true if map contains the specified key.
  • Get: retrives values attached to a certain key.
  • isEmpty: returns true if the map has zero elements.
  • Keyset: returns a set that contains all the keys in the map.
  • Put/putAll: associates the specified value(s) with the specified key(s)
  • size: returns the number of key-value pairs in the list.
  • Remove: removes the mapping for the specified key.
  • values: returns a list of values in the map in arbitrary order.



Apex access modifiers

Apex allows to use the below modifiers when  defining methods and variables.

  • Private
  • Protected
  • Public
  • Global

To use the private, protected, public, or global access modifiers, use the following syntax:

  • [(none)|private|protected|public|global] declaration

private :

  • This is the default, and means that the method or variable is accessible only within the Apex class in which it is defined.
  • If an access modifier is not specified, the method or variable is private.

protected :

  • This means that the method or variable is visible to any inner classes in the defining Apex class.
  • This access modifier can only be used for instance methods and member variables.

public :

  • This means the method or variable can be used by any Apex in this application or namespace.

global :

  • This means the method or variable can be used by any Apex code that has access to the class, not just the Apex code in the same application.
  • If a method or variable is declared as global, you must also declare the class that contains it as global.