Difference Between Long and Int Data Types in Java

Bishal Awasthi Dec 25, 2021 Jul 04, 2021
  1. the int and long Data Types in Java
  2. the Integer and Long Wrapper Classes in Java
  3. Differences Between int and Integer and long and Long in Java
Difference Between Long and Int Data Types in Java

This article will discuss the differences between the int and long data types in Java and explain the wrapper classes Integer and Long. We will understand the differences between these data types and the wrapper classes and will be able to decide which one to use according to the scenario.

the int and long Data Types in Java

The int data type is a primitive data type in Java that stores a whole number. It means that int specifies the size and type of the variable value. But, it cannot call any methods. The size of int is 4 bytes. It can store the values from -2,147,483,648 to 2,147,483,647.

The long data type is also another primitive data type in Java with 8 bytes. It has a wider range than int to store a whole number. The long data type is used to hold the larger whole number that int cannot hold. It can store the values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

For example, create a variable num1 with the int data type and the num3 with the long data type. Assign a value of 100 to num1 and 15000000000L to num3. Print the variables in the console. Again, create another variable, num2 with the same data type, and assign a value of 2147483648. Then, print the num2 variable. Note that these lines of code should be written inside the main method inside a class.

The first code example successfully executes, but the second example runs into an error. It is because the value 2147483648 exceeds the range for the int data type. We write L after the long data type’s value to denote a long value.

Example Code:

int num1 = 100;
long num3 = 15000000000L;
System.out.println(num1);
System.out.println(num3); 

Output:

100
15000000000

Example Code:

int num2 = 2147483648;
System.out.println(num2);

Output:

Main.java:3: error: integer number too large
 int num2 = 2147483648;
 ^
1 error

the Integer and Long Wrapper Classes in Java

A wrapper class is a specific type of class whose objects can store the primitive data types. We will discuss the Integer and Long wrapper classes. We can create the wrapper object from the wrapper class, and it lets us use different methods associated with the object.

For example, create a wrapper class Integer’s object myInt and assign a value of 50. Create another object myLong from the wrapper class Long and assign a value 326162165161616161. Print out the variables myInt and myLong. Then create, two objects of the String wrapper class as myString1 and myString2. Use the method toString() with each of the myInt and myLong objects. Finally, print the length of the objects myString1 and myString2 calling the length() method.

In the example below, we created objects from the wrapper classes Integer, Long, and String. We called the methods like toString() and length() with the objects. The toString() method converts the values into a string, and we can use the length() method to calculate their length. If we try to access these methods from the primitive data type, we cannot do it. Thus, wrapper classes let us create the wrapper objects to use the methods for various operations.

Example code:

Integer myInt = 50;
Long myLong = 326162165161616161L;
 
System.out.println(myInt);
System.out.println(myLong);
 
String myString1 = myInt.toString();
System.out.println(myString1.length());
 
String myString2 = myLong.toString();
System.out.println(myString2.length());

Output:

50
326162165161616161
2
18

Differences Between int and Integer and long and Long in Java

The major difference between the int or long and Integer and Long is the data types are primitive types while the wrapper classes are non-primitive types. A primitive data type holds a specific value, while the wrapper class’s objects are only the pointers to the location of the data stored.

Integer myInt = new Integer(5);
int num = 5

The myInt object above is just a pointer to the data 5 location. The memory where the myInt object is stored contains the memory address of the value 5 location. It is only a reference to the location of the value. Meanwhile, the memory where the variable num is stored contains the value 5.

This concept can be visualized with the null value. We can set the myInt object with a null value, but we cannot do the same with the num variable. An object can refer to a null address because objects are reference types. The int data type cannot contain a null value because int is primitive and cannot reference. So, when we assign null to an object, it outputs as null, but when we assign it to an int type, an error will be thrown.

Example Code:

Integer myInt = null;
System.out.println(myInt);

Output:

null

Example Code:

int num = null;
System.out.println(myInt);

Output:

Main.java:6: error: incompatible types: <null> cannot be converted to int
int num = null;
 ^
1 error

Related Article - Java Long

Related Article - Java Integer