Introduction to Integer Variables: Android Studio Crash Course (Free Tutorial)

Have you ever needed to represent a number when coding your application? Well, you’re in the right place! Integers are one of the most common ways to represent numbers, no matter the coding language. Today we’re discussing initializing and declaring integers in Android Studio.

If you need an introduction to coding and want to learn for FREE, check out our 30-minute beginners course here:

To follow along with this example in Android Studio, go into Project view. Then go to app > java > (topmost) com.example.zebra.demo > MainActivity.

Integers are whole numbers (not containing decimal places). They can take negative or positive values. Today we’ll be looking at three datatypes used to declare and initialize integers:

1. The most common method is through the use of the keyword int. Beneath setContentView(R.layout.activity_main);, let’s use this keyword to declare integers named numbers1, 2, and 3. For this example, we’ll assign number1 and number2 the values 0 and 9:

int number1 = 0, number2 = 9, number3;

You can also do operations, such as addition, with integers. On a new line, make number3 equal to number1 + number2.

number3 = number1 + number 2;

2. Another way to declare integers is with the Integer data type. This data type is used when there is the presence of integer arrays (lists of integers). The Integer data type takes more memory from your application than int.

Use the keyword Integer to declare two integers named number4 and number5. On a line beneath that, make number4 equal to number1 + number2.

Integer number4, number5;
number4 = number1 + number2;

Evidently, you can mix up the Integer and int datatypes when coding operations.

3. The BigInteger datatype is used for integers that span more than 100 digits. For integers that are within -2 billion and +2 billion, you can use int and Integer datatypes. Otherwise, you have to use BigInteger.

Using the datatype BigInteger, let’s declare an integer number6 and assign it the value 1000000000000. To do so, use the following format:

BigInteger number6 = new BigInteger("1000000000000");

Let’s add some more integers beneath that line by first changing the semi-colon to a comma. Declare two integers number7 and number8. Set number7 equal to a random value such as 23999999999:

number7 = new BigInteger("23999999999"), number8;

In order to set the value of number8 equal to the sum of number6 and number7, we have to use a built-in function called add. So far, your line should look like this:

number8 = number6.add();

We must now pass as a parameter a new BigInteger value. You can do this by adding number7 into the parentheses of the add function.

You have just implemented some integers in Android Studio! Because numbers often show up in applications, this is a pretty necessary concept to grasp. If you want to learn even more coding for FREE, check out our 30-minute beginners course here:

Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.