Hit enter after type your search item

5 examples of Java int, Integer and long data 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 example long example

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 wrappers classes in Java. The int has Integer and long has Long wrapper class.

Integer class example

There are certain scenarios when you need working 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:

See online demo and code

The Java code for int example:


The result:

Java int

You can see, the first line is used only to declare two int Java 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 an array for displaying the array elements.

See online demo and code

The code:


The Output:

int for loop

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 much larger value than int type, that 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:

See online demo and code

The Java code:


Output of the above code:

Java long

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

A few main points about the int and long primitive 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 is a numeric primitive data types in Java.
  • The int takes 32-bit memory.
  • The maximum value that an int variable can store is 2,147,483,647.
  • The minimum value of int variable can be – 2,147,483,648.
  • The default value of int is 0.
  • The int has a wrapper class Integer in Java.
  • The int can’t store null value while an integer can.
  • The advantage of integer class can be using in collections with generics or object references that you can’t with primitive type.
  • Just like other classes, you have to create objects of integer class that contains single field i.e. int.

The long (primitive type) and Long class:

  • The long is a numeric data type in Java.
  • This is also the primitive type.
  • The long type takes 64 bits of memory.
  • The maximum value that a long type variable can store is 9,223,372,036,854,775,807L.
  • The minimum value is -9,223,372,036,854,775,808L.
  • The wrapper class for long is Long.

An example of using the integer class

In this simple example of using 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 other two. Have a look:

integer

See online demo and code

The code with Integer class:

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:

See online demo and code

The ArrayList with integers:


The result:

integer arraylist

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 integer class in above section; for larger values.

This div height required for enabling the sticky sidebar