Primitive Data Types in Salesforce Apex

Apex uses the same primitive data types as the SOAP API. All primitive data types are passed by value, not by reference.

All Apex variables, whether they’re class member variables or method variables, are initialized to null. Make sure that you initialize your variables to appropriate values before using them. For example, initialize a Boolean variable to false.

  • Integer: A 32-bit number that does not include decimal point. Integers have a minimum value of -2, 147,483648 and maximum value of 2,147,483648.
    For example:

     Integer i = 1; 
  • Long: A 64 bit number that doesn’t includes a decimal point. Long has a minimum value of -2^63 and a maximum value of 2^63-1.
    For example:

    Long l = 2147483648L;
  • Double: A 64 bit number that doesn’t includes a decimal point. Long has a minimum value of -2^63 and a maximum value of 2^63-1.
    For example:

     Double d = 3.14159; 
  • Decimal: A number that includes a decimal point. Decimal is an arbitrary precision number. Currency fields are automatically assigned the type decimal.
    For example:

     Double d = 256.32; 
  • String: Strings are set of characters and are enclosed in single quotes. They store the text values such as name or an address.
    For example:

     String str = 'Biswajeet Samal'; 
  • Date: A value that indicates a particular day. Date values contain no information about time. Date values must always be created with system static method.
    For example:

     Date myDate = Date.newinstance(2015, 09, 14);
    Output: 2015-09-14       00:00:00
    
  • Time: A value that indicates a particular time. Time values must always be created with a system static method.
    For example:

    Time tm = newInstance(10,12,5,11);
    Output: 10:12:05
    
  • Date Time: These are data types associated with dates and times along with Date data type. The time data type stores times (hours, minutes, second and milliseconds). The Date data types stores dates (Year month and day). The time data type stores both dates and times.
    Each of these classes has a newInstance method with which we can construct particular date time values.
    For example:

    Date dt = Datetime.now();
    
  • Id: Any valid 18-character Force.com record identifier.
    For example:

    ID id='00910000004T2AGAA0';
    

    If you set ID to a 15-character value, Apex converts the value to its 18-character representation. All invalid ID values are rejected with a runtime exception.

  • Boolean: A value that can only be assigned true, false, or null.
    For example:

    Boolean isValid = true;
    
  • Blob: A value that can only be assigned true, false, or null.
    For example:

    It stores files data in binary format.