fbpx

Free Salesforce Developers Tutorials >

Chapter 3 - Apex Datatypes and Operators >

Integer Data Type in APEX

Integer Data Type in APEX

What You’ll Learn


S2 Labs

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

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Download Now

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *