Experience Salesforce
Integer Data Type in APEX
What You’ll Learn
- Types of Integer Datatype
- Syntax of Integer Datatype
- Some Things to Remember
Topics
- Apex Data Types
- Integer Data Type in APEX
- Floating Point Data Type in APEX
- String Data Type in APEX
- Date Datatypes in APEX
- Time and DateTime Data Types in APEX
- Boolean Datatype in Salesforce
- ID & Blob Datatypes in APEX
- What is the Rule of Conversions in Apex?
- Enums in APEX
- sObjects in Salesforce
- Generic sObjects in Salesforce
- Collections in APEX
- Lists in APEX
- List Array Notation in APEX
- Lists Initialization in APEX
- Nested Lists in APEX
- Sets in APEX
- Sets Initialization And Methods
- Maps in APEX
- Map Initialization Methods in APEX
- Operators in Apex and Their Types
- Shorthand Operator
- Equality Operator
- Relational Operators(<,>,<=,>=)
Integer datatypes have variables that can store negative and positive values. The integer data type in Apex is intended to hold total numbers that are devoid of decimal points and span from negative to positive values. This 32-bit Integer is frequently utilized in loops for counting or iterating. Its value range is between -2,147,483,648 and 2,147,483,647.
Example of Integer Data Type:
Imagine a scenario in a Cricket IPL game where we need to store the score of a particular game. In this case, an integer variable, cricket_score, can be utilized for this purpose. The cricket_score can be updated to reflect whenever a player adds some more runs during the play.
For example
integer cricket_score=0;
cricket_score=cricket_score+4;
Types of Integer Datatype
In Apex, there are actually just two data types specifically designed to store integer values:
- Integer (4 bytes): This is the most commonly used data type for whole numbers. It can store values from -2,147,483,648 to 2,147,483,647.
- Long (8 bytes): This data type is used for storing larger whole numbers than can fit in an Integer. Its range is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Syntax of Integer Datatype
Integer intValue = 343;
If the literal goes outside the integer range, append l or L at the end of the literal because, by default, a number is treated as an integer.
Eg.
Long largeValue = 3434343434L;
If the value is not in the range of type defined, then the compiles time error is generated.
Choosing the Right Integer Datatype:
Some Things to Remember
- Apex doesn’t have a separate data type for short integers (e.g., 16-bit integers). If memory optimization is a critical concern, you can explore alternative approaches like using primitive data types in Java through the JVM interoperability features of Apex, but this is generally not recommended for most Salesforce development scenarios.
- When working with calculations or mathematical operations involving integers, be mindful of potential overflow situations where the result might exceed the data type’s range. Apex provides mechanisms to handle overflow exceptions, but it’s always good practice to consider the potential range of values during development.
Need Extra Support? Our FREE study materials have got you covered.
Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.