Variables and Data types in Salesforce
What is variables?
variables is nothing but a location into the memory where your data is going to store.
What is data types?
What type of memory is occupied by the variable and how much data is going to store into a variable is gorverned by the data types.
Data types are used to define variables and methods in classes. In Apex, all variables & expressions have one of the below data types. Those are as follows
Primitive data types (Integer, Boolean, String, …… etc.)
Enum (an enumerated list)
sObjects (sObject, or Account, contact, object__c….)
Collection (list, set, map)
Null (for the null constant. Which can be assigned to any variable.
An object created from the user – or system-defined classes.
Example of primitive data types
I’m working with two language right now(Java and Apex.),So I wanted to compare and contrast the different primitive data types for both language.This is great, because Apex is based on java, and share many of the same data types.
There are some exceptions:
Some data types exist in Java but not Apex(e.g. Short), and others are in Apex, but not in Java(e.g., ID).
Some of the names for data types(and thus how you declare them) are different Java then in Apex. e.g. “int” and “char” in Java are equal to “Integer” and “String“
The table below list the various primitive data types in both Java and Apex, with Example in Apex of how you might declare a variable of that typeand assign a value to it.
Java | Apex | |||
---|---|---|---|---|
Data Type | Description | Data Type | Description | Apex Code Examples |
Boolean & Character Data Types | ||||
Boolean | Think of this as a yes/no question. It can store a value true, false or null | Boolean | true, false or null | Boolean b = false; |
Char | Can store up to 64,435 characters | String | No set limit | String str = “Hello Apex”; |
Numerical Data Types without Decimal Point(i.e., Whole Numbers) | ||||
Byte | -128 to 127 | Integer | -2,147,483,648 to 2,147,483,647 | Integer i = 10; |
Short | -32,768 to 32,767 | |||
int | -2,147,483,648 to 2,147,483,647 | |||
Long | a really big negative number to a really big positive number | Long | a really big negative number to a really big positive number | -3123476872302339 to 3123476872302339 |
Numerical Data Types with Decimal Points(i.e., fractional numbers) | ||||
Float | Used in numbers with decimal point. Useful when working with fractional numbers but where don’t need a great degree of precision. | Decimal | A number that includes a decimal point. Decimal is an arbitrary precision number. In Apex, currently fields are automatically assign the type Decimal. Also Decimal has a build-in methods and rounding options. | Decimal d = 20.3232; |
Double | Also used when working with number that need to used a decimal point. but more accurate then float.In Java, use double when working with currency | Double | Also a number that includes a decimal point. From a really big negative number to a really big positive number. | Double d = 3.14329; |
ID | A valid 18 digit character Force.com record identifier. | ID id = ‘00340000344WSAF3’; | ||
Blob | Used to holds files(e.g., images, documents, emails.) | |||
Note that Java does not include a date/time primitive data type, Instead you create and use dates through Date class. | Date | Used to indicate a particular day | Date todayDate = Date.today(); Output : 2016-03-04 12:20:23 (in year, month, date, hour, minutes, seconds) |
|
DateTime | Used to indicate particular day and time | DateTime CurrentDateTime = DateTime.now(); Output : 2016-03-04 12:20:23 (in year, month, date, hour, minutes, seconds) |
||
Time | Used to indicate particular time | Time CurrentTime = DateTime.now(); Output : 12:20:23:034 (in hour, minutes, seconds and milliseconds) |
NOTE : Null is actually the absence of a value, which should be confused with an empty String or Zero, which represent actual values.
I wish I could give better explanation of when to use Decimal vs Double – the best I can say is if you want to store currency or percentages need to access to more methods and rounding options then are available for Double, then use Decimal. Otherwise use Double.
These example create dates and time using the build-in method the Date and DateTime Apex classes.
How does all this relate to Salesforce Field Types?
So you can see that we have quite a few basic building blocks
to work with here.For those of you Salesforce Awesome Admins.We can equate the Apex data types of field types in Salesforce as follows:
Salesforce Field Type | Apex Data Type |
---|---|
Auto-Number | String |
Checkbox | Boolean |
Currency | By default assign as Decimal |
Date | Date |
Date/Time | DateTime |
String | |
Number | Integer or Double, depending on if you set the decimal place to zeroor more then zero when you create the field. |
Percentage | By default assign as Decimal |
Phone | String |
Text, Text(Encrypted), Text Area, Text Area(Long), Text Area(Rich) | String |
URL | String |
Wrapping things up
There is more I can say about primitive data types, but that’s about all I could possibly hope for anyone to read through. Together we’ll circle back to this topic in this future when we will discuss variables with non-primitive data types.
For now you know that:
Variables are like containers that hold things (values)
Variables must, at a minimum, have a name and a data type
Data types comes in two flavours : primitives and non-primitives
primitives data types can’t be broken down further (e.g. Lists contain elements)
Java and Apex have very similar primitive data types
Enum (an enumerated list)
An enum is an abstract data type with values that each take on exactly one of a finite set of identifiers that you specify. Enums are typically used to define a set of possible values that don’t otherwise have a numerical order, such as the suit of a card, or a particular season of the year. Although each value corresponds to a distinct integer value, the enum hides this implementation so that you don’t inadvertently misuse the values, such as using them to perform arithmetic. After you create an enum, variables, method arguments, and return types can be declared of that type.
Note Unlike Java, the enum type itself has no constructor syntax.
To define an enum, use the enum keyword in your declaration and use curly braces to demarcate the list of possible values. For example, the following code creates an enum called Season:
Example of Enum :
public enum Season {WINTER, SPRING, SUMMER, FALL}
sObjects (sObject, or Account, contact, object__c….)