Java int, Integer and long/Long Data Types

A visual illustration comparing the Java primitive int and Integer Class with the long and Long class. learn the differences and main points about both types.

The int and long data types in Java

  • Java provides a number of numeric data types while int and long are among those.
  • The int and long are primitive data types.
  • The int takes 32 bits or four bytes of memory while long takes 64 bits or 8 bytes.
int x = 5;

Integer y = new Integer(5);

long x = 4000000000L;

Long y = new Long(4000000000L);
  • So, if your program is supposed to store small numbers then use an int type.
  • If the number is greater than +- 2 billion (around) then use a Java long type variable.
  • Both int and long types have wrapper classes in Java.
  • The int has an Integer and long has a Long wrapper class.

There are certain scenarios when you need to work with objects rather than using primitive data types for storing numeric values.

I will explain this in the later part of this tutorial. First, have a look at a few examples of declaring and using the int and long data types.

An example of using Java int type

This is how you may declare and use the int type variables in Java programs. See this example where I have declared and assigned values to int types variables in two ways:

Java code for int example:

public class java_variables {

    public static void main(String []args) {

      //Declaring int type variables

        int num_a,sum_c;
        int num_b= 500000;

        num_a= 150000;

        //Getting sum of two numbers

        sum_c = num_a + num_b;
     System.out.println("The sum of two int variables is = " + sum_c);
  }

}

The result:

The sum of two int variables is = 650000

You can see that the first line is used only to declare two int type variables.

The value is assigned to the num_a after declaring it. In the second line, the num_b is declared and a value is assigned at the time of declaration.

The example of using int variable in for loop

See this example where an int type variable is used in a for loop. The variable is declared inside the loop and used with a Java array for displaying the array elements.

The code:

public class java_variables {

                   public static void main(String []args) {
                        int[] Intarr = {5,10,15,20,25,30,35};

                        //Declaring int type variable inside the for loop

                        for (int ArrItem : Intarr) {

                            System.out.println("The numeric array value: " + ArrItem);

                        } 
                    }

}

The Output:

The numeric array value: 5The numeric array value: 10
The numeric array value: 15
The numeric array value: 20
The numeric array value: 25
The numeric array value: 30
The numeric array value: 35

Using the long Java data type example

Just like the int type variables, the long type is declared. You may assign a value at the time of declaration or after that.

As mentioned earlier, the long type takes 64 bits of memory, so may store a much larger value than the int type, which takes 32 bits.

Have a look at the following example where long type variables are declared and assigned the values and used in the calculation. For demonstration, the values of long variables are kept a little higher than the limit of the int type:

Java code:

public class java_variables {

    public static void main(String []args) {

                //Declaring long type variables

        long num_a,sum_c;
        long num_b= 2147483649L;

        num_a= 2147483649L;

        //Getting sum of two long numbers
       sum_c = num_a + num_b;

     System.out.println("The sum of two long variables is = " + sum_c);

  }

}

Output of the above code:

The sum of two long variables is = 4294967298

The values for the long type variable are used with “L” at the end.

A few main points about the int and long types

Before going into examples of using the Java Integer and Long classes, let us have a look at a few important points of int and long data types and Integer/Long classes.

The int and integer

The int The Integer
The int is a numeric primitive data type in Java. The wrapper class for int is called Integer.
The int takes 32-bit memory. The Integer is an object and can be used in collections.
The maximum value that an int variable can store is 2,147,483,647. The Integer can store null values.
The minimum value of an int variable can be -2,147,483,648. The Integer provides additional methods for working with integers.
The default value of an int is 0. The Integer has methods like parseInt() for conversion.
The int has a wrapper class Integer in Java. You have to create objects of the Integer class for single fields.
The int can’t store null value. The Integer can store null values.
The Integer class can be used in collections with generics.
Objects of the Integer class contains a single field, i.e., int.

The long (primitive type) and Long class:

The long (primitive type) The Long class
The long is a numeric primitive data type. The Long is the wrapper class for long in Java.
The long type takes 64 bits of memory. The Long is an object and can be used in collections.
The maximum value that a long type variable can store is 9,223,372,036,854,775,807L. The Long can store null values.
The minimum value is -9,223,372,036,854,775,808L. The Long provides additional methods for long.
The Long class has methods like parseLong() for conversion.
The Long class can be used in collections with generics.
Objects of the Long class contains a single field, i.e., long.

An example of using the integer class

In this simple example of using the Integer class, three variables are declared just like the above examples. Two are assigned the values and the third Integer variable is the sum of the other two. Have a look:

Code with Integer class:

public class java_variables {

    public static void main(String []args) {

        //Declaring Integer type variables

        Integer num_a,sum_c;
        Integer num_b= 214748364;

        num_a= 214748364;

        //Getting sum of two Integer numbers
        sum_c = num_a + num_b;

     System.out.println("The sum of two Integer variables is = " + sum_c);
  }
}

Output:

The sum of two Integer variables is = 429496728

Example of using Integer in ArrayList collection

As mentioned earlier, the Integers class can be used with generics. For example, if you want to create an ArrayList of int numbers, you can’t do it by using the primitive type int. As such, the generic types must be of type objects, so you may use the Integer there.

In the following example, an ArrayList is created with five numeric elements. The ArrayList is declared with Integer type, so the elements can be numbers only. This is how the Integer is used:

The ArrayList with integers:

import java.util.*;

public class java_variables {

    public static void main(String []args) {

        ArrayList<Integer> IntegerArrLst = new ArrayList<Integer>(); //Declaring ArrayList with Integer

        IntegerArrLst.add(5);
        IntegerArrLst.add(10);
        IntegerArrLst.add(15);
        IntegerArrLst.add(20);
        IntegerArrLst.add(25);

        //Displaying array list

        for (int IntgerArrItem : IntegerArrLst) {

                 System.out.println("The Integer item in ArrayList: " + IntgerArrItem);

              }
}           
}

The result:

The Integer item in ArrayList: 5The Integer item in ArrayList: 10
The Integer item in ArrayList: 15
The Integer item in ArrayList: 20
The Integer item in ArrayList: 25

If you try using the primitive type int in place of Integers in the above example, an error will be generated.

Similarly, you may use Integer in other collection types like vectors.

The Long class

For the long primitive type, the wrapper class in Java is Long. You may apply the same examples as used for the integer class in the above section; for larger values.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!