Apex Data Types Simplified
What is Apex Data Types and why do we need them in the first place?
Suppose you went to a market and bought awesome clothes, shoes and food items. When you return back home you can keep clothes in a separate wardrobe, shoes in another cupboard and food items in kitchen cupboards ideally.
Similarly to store different types of data we required a Data Type declaration. Such as String data should be stored in a String Data Type variable and likewise Integer data should be stored in Integer Data Type Variable.
Also, wanted to share that there are languages such as Javascript that don’t require a Data Type declaration while storing values in a variable. They are handled at runtime only. However, our beloved Apex says that please provide the Data Type of the variable which you will be using.
In Apex, all variables and expressions have a data type, such as sObject, primitive, or enum.
Primitive Data Types:
Let’s see different primitive data types and the value they can hold with a game of Cricket π
Boolean
A value that can only be assigned true, false, or null
Boolean isCricketMatchToday = true;
DateTime
A value that indicates a particular day and time, such as a timestamp.
Datetime matchSchedule = DateTime.newInstance(2021, 6, 29, 9, 0, 0); // (Year, Day, Month, Hour, Minutes, Seconds)
Date
A value that indicates a particular day. Unlike Datetime values, Date values contain no information about time.
Date finalsDate = Date.newInstance(2021, 7, 9); // (Year, Month, Date)
Time
A value that indicates a particular time.
Time lunchBreak = Time.newInstance(14, 0, 0, 0); // ( Hour, Minutes, Seconds, MilliSeconds)
Id
Any valid 18-character Lightning Platform record identifier
Id scoreIdInSalesforce = '0Q02g0000006CNdCAM'; // record id in salesforce
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
Integer matchScore = 150;
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 the range provided by Integer.
Long ticketSold = 100000000; // Very large non decimal value
Double
Double can contain fractional values.
Double runRate = 12.5;
Decimal
Decimal is better than Double for maintaining precision for floating-point calculations. Itβs preferable for currency fields.
Decimal ticketsEarnings = 2000000000.4555;
String
Any set of characters surrounded by single quotes.
String currentBatsman = 'MS Dhoni';
Blob
A collection of binary data stored as a single object. You can convert this data type to String or from String using the toString and valueOf methods, respectively
Blob tournament = Blob.valueOf('World Cup');
Object
Any data type that is supported in Apex. Apex supports primitive data types (such as Integer), user-defined custom classes, the sObject generic type, or an sObject specific type (such as Account). All Apex data types inherit from Object.
// Handy when you are not sure which data type will be passed so you provide as an Object type to hold any other type
Object genericeType = currentBatsman;
Object genericType2 = runRate;
sObject:
It refers to an object which is created in Salesforce. It could be a standard object or any custom object.
sOBject genericObject = new Account(); // standard object
sObject anotherGenericObject = new Cricket__c(); // custom object
Enums:
An enum represents a group of constants (unchangeable variables, like final variables).vTo create an enum, use the enum keyword, and separate the constants with a comma.
public enum CricketGroups {
A, B, C, D
}
Collections:
Collections in Apex can be lists, sets, or maps.
You can refer Collection in depth over here – Apex Collections
Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below π