Experience Salesforce

Integer Data Type in APEX

What You’ll Learn

  • Types of Integer Datatype
  • Syntax of Integer Datatype
  • Some Things to Remember

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:

  1. 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.
  2. 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.
salesforce-developer
Next Topic

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.

cts-img
Rakshabandhan Sale