Java

How to Convert long to int in Java

In Java, there can be certain situations where the developer needs to save memory effectively. For instance, allocating the data type having a relatively smaller range of values or getting rid of the extra values. In such instances, the conversion of “long” to “int” in Java comes into effect by streamlining the arithmetic operations, code complexity, and takes comparatively less execution time.

This article will elaborate on converting “long” to “int” in Java.

What are “long” and “int” in Java?

The “long” type in Java ranges from “-9223372036854775808” to “922337203685477580” and is allocated “64” bits. The “int” type, however, occupies “32” bits ranging from “-2147483648” to “2147483647”. This implies that any number of “int” types can easily be supported by a “long” type but it is not always the case most of the time.

How to Convert “long” to “int” Using Java?

To convert “long” to “int” in Java, apply the following approaches:

Approach 1: Convert long to int in Java Using the “Math.toIntExact()” Method

The “Math.toIntExact()” method in Java gives the “int” value from the provided “long” argument. This method can be applied to simply pass the “long” value as its argument and return it as an integer.

Syntax

Math.toIntExact(long val)

In this syntax, “long val” refers to the value that needs to be returned as an “int”.

Example
Let’s overview the following example:

public class longtoint2 {
 public static void main(String[] args) {
  long val1 = 8624675L;
  long val2 = 3482398L;
  int updVal1 = Math.toIntExact(val1);
  int updVal2 = Math.toIntExact(val2);
  System.out.println("The integer value is: "+updVal1);
  System.out.println("The integer value is: "+updVal2);
}
}

In this code snippet, perform the following steps:

  • Firstly, initialize the two “long” values.
  • Note: The specified “L” in the values represent and differentiate the “long” values from “int” values.
  • After that, apply the “Math.toIntExact()” method twice and accumulate both the initialized long values, as its parameter to convert them into integers.
  • Finally, log the converted integer values on the console.

Output

In the above outcome, it can be analyzed that the corresponding integer values are displayed.

Note: The conversion (long to int) process works fine when the value of the “long” is less than or equal to the maximum value of “int”, i.e., (2147483647). In the other case, an error is encountered and there can be data loss as well.

Demonstration
Let’s observe the discussed concept leading to the limitation:

In the above example, simply increase the latter “long” value and perform the conversion. This will lead to encountering the “Integer overflow” limitation since the converted value exceeds the “int” max limit.

Approach 2: Convert long to int in Java Using “Narrow Typecasting”

Typecasting” corresponds to assigning a value of one primitive data type to another. In this case, the “narrowing typecasting” comes into effect since the higher data type, i.e., “long” is transformed into the lower data type “int”. This can be achieved by simply placing the value that needs to be transformed followed by the desired type in parentheses.

Example
The following example explains the discussed concept:

public class longtoint {
 public static void main(String[] args) {
  long val1 = 647467L;
  long val2 = 348239L;
  int updVal1 = (int)val1;
  int updVal2 = (int)val2;
  System.out.println("The integer value is: "+updVal1);
  System.out.println("The integer value is: "+updVal2);
}
}

In the above lines of code:

  • Likewise, initialize the two “long” values.
  • In the next step, apply the “narrow typecasting” approach by placing the value that needs to be converted followed by the desired type, i.e., “int”.
  • Lastly, display the converted “int” values on the console.

Output

In this output, it can be seen that the “long” values are converted into “int” values, appropriately.

Approach 3: Convert long Object to int in Java Using the “intValue()” Method

The “intValue()” method in Java returns the value of the associated number in the form of “int”. This method can be implemented to simply transform the associated long “objects” into integers.

Example
Go through the below-provided example to perform the conversion:

public class longtoint3 {
 public static void main(String[] args) {
  Long val1 = new Long(8624675L);
  Long val2 = new Long(3482398L);
  int updVal1 = val1.intValue();
  int updVal2 = val2.intValue();;
  System.out.println("The integer value is: "+updVal1);
  System.out.println("The integer value is: "+updVal2);
}
}

In the above code block:

  • First of all, create two “long” objects via the “new” keyword and the “Long()” constructor, respectively.
  • In the constructor parameters, specify the long values that need to be transformed into “int”.
  • Now, associate the “intValue()” method with both of the created objects and convert the accumulated “long” values into “integers” and display them.

Output

The above outcome implies that the object values are converted into “integers” appropriately.

Conclusion

To convert long to int in Java, apply the “Math.toIntExact()” method, the “Narrow Typecasting” approach, or the “intValue()” method. These approaches transform the initialized long value and object into an integer provided that the “long” value that needs to be converted does not exceed the maximum value of “int”. This blog is guided to converting long to int using Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.