Data Type and different Data Types are available in Apex



A data type in a programming language is a set of data with values having predefined characteristics. Examples of data types are: integer, floating point unit number, character, string, and pointer. Usually, a limited number of such data types come built into a language. The language usually specifies the range of values for a given data type, how the values are processed by the computer, and how they are stored.

Apex is a strongly typed, object-oriented programming language. Just like any other  programming language, Apex has variety of data types that you can use.

  1. Primitive Types - This data types include String, Integer, Long, Double, Decimal, ID, Boolean, Date, Datetime, Time and Blob. All these data type variables are always passed by value in methods.  Another point to note is in Apex, all variables are initialized to null when declared. You must explicitly initialize to non-null values before using.

Data Type
Description
Blob
A collection of binary data stored as a single object.
Boolean
A value that can only be assigned true, false, or null. For example:
Boolean isWinner = true;
Date
A value that indicates a particular day. Unlike Datetime values, Date values contain no information about time. Date values must always be created with a system static method.
Datetime
A value that indicates a particular day and time, such as a timestamp. Datetime values must always be created with a system static method
Decimal
A number that includes a decimal point. Decimal is an arbitrary precision number. Currency fields are automatically assigned the type Decimal.
Double
A 64-bit number that includes a decimal point. Doubles have a minimum value of -263 and a maximum value of 263-1. For example:
Double d=3.14159;
ID
Any valid 18-character Force.com record identifier. For example:
ID  ='00300000003T2PGAA0';
Integer
A 32-bit number that does not include a decimal point. Integers have a minimum value of  -2,147,483,648 and a maximum value of  2,147,483,647. For example:
Integer i = 1;
Long
A 64-bit number that does not include a decimal point. Longs have a minimum value of -263 and a maximum value of 263-1. Use this data type when you need a range of values wider than those provided by Integer. For example:
Long l = 2147483648L;
String
Any set of characters surrounded by single quotes. For example,
String s = 'The quick brown fox jumped over the lazy dog.';
Time
A value that indicates a particular time. Time values must always be created with a system static method.
  1. sObject Types - This is a special data type in Apex. sObject is a generic data type for representing an Object that exists in Force.com. It could be Standard object like Account, Opportunity etc., or Custom object that you define.  Following are some of the examples of sObject variables -
Sobject s = new Account();

Account a = new Account();
CustomObject__c c = new CustomObject__c();

As you can see above, your custom objects have an extension of __c to distinguish from the Force.com standard objects. Fields from the sObject variable can be accessed using the dot notation. For example
c.Name = 'Test Name';
  1. Collections - Apex has 3 types of collections. Lists, Sets and Maps.
  • list is like an array, a sequential collection of elements with first index position as zero. List can contain elements of primitive types, sObjects, user-defined objects, Apex objects or even other collections. A list can contain up to four levels of nested collections. List can contain duplicate elements.
  • set is also a collection of elements and elements can be of any data type. Unlike list, set contains unique elements and elements in set are not in any specific order.
  • map is a collection of key-value pairs. Keys and values can be any data type. Keys are unique and map elements must be accessed by the key as the order of  map elements are not reliable.


  1. Enums - Just like in other programming languages, Enum type represents a fixed set of named constants.
An enum is a data type which contains fixed set of constants. It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY),directions (NORTH, SOUTH, EAST and WEST) etc. The enum constants are static and final implicitly.
After you create an enum, variables, method arguments, and return types can be declared of that type.

ex:- 
public enum Season {WINTER, SPRING, SUMMER, FALL}
Once you define your enumeration, you can use the new enum type as a data type for declaring variables. The following example uses the Season enum type that is defined first and creates a variable s of type Season. It then checks the value of the s variable and writes a different debug output based on its value. Execute the following:
public enum Season {WINTER, SPRING, SUMMER, FALL}
Season s = Season.SUMMER;
if (s == Season.SUMMER) {
// Will write the string value SUMMER
System.debug(s);
} else {
System.debug('Not summer.');
}

Comments

Popular posts from this blog

Service Cloud for Lightning Experience - All Steps Q&A [Salesforce][Trailhead]

Salesforce Process Builder in Brief (Learning/Interview)

Cloud Computing

Copado Winter 21 Administrator Maintenance Dumps

Einstein Analytics Data Preparation Specialist