Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Int and Long are data types used to store numeric values without decimals in programming. While both can hold large numbers, Long accommodates a significantly wider range. This article explores their representation, type casting for data type conversion, and methods to convert Long to Int in Java, including explicit casting, toIntExact(), and intValue().
What are Int and Long data types?
Int (for integer) and Long data types store numeric values (without decimal).
They hold larger numbers than the short data type.
Int: Int representation uses 32-bit signed two's complement integers.
Int can store values ranging from -231 to a maximum of 231-1.
Long:
The long data type used 64-bit two's complement integer representation.
The long data type can store numbers between a minimum value of -263 and a maximum value of 263-1.
From ranges, it is clear that the Long datatype is used to store larger numbers than the Int data type.
We may have some need where we have to convert long to int in Java. This is called Narrow Type Casting.
What is Type Casting?
We might want to convert one datatype into another.
Suppose we have taken input from a user, which is a number, but in string format. Here, we have to convert the string input to a number( int/long) before processing so that arithmetic operations can be done on the input.
Here, we will have to convert the data type of the entered string into the desired data type.
Type casting means converting one data type into another datatype.
There are two types of Type Casting:
Widening Type Casting
Narrow Type Casting
Widening Type Casting
When a smaller data type converts to a large one. Widening type casting does not involve loss of information and is hence safe to be done by the compiler implicitly. Implicit casting is possible only for the compatible data type. Example
public class MyClass {
public static void main(String args[]) {
int x = 10;
long y = x;
System.out.println("Widening Type Casting !");
System.out.println("Value of y is "+y);
}
}
You can also try this code with Online Java Compiler
Java implicitly converts an integer variable 'x' to a data type variable long before assigning it to y.
Output
Narrow Type Casting
When a variable of a higher data type is converted to a smaller data type, then it is known as Narrow Type Casting.
Conversion of long to int in Java is an example of Narrow Type Casting. It involves loss of information, and hence the compiler will not do it implicitly, even for the compatible data type. Example
public class MyClass {
public static void main(String args[]) {
long x = 10;
int y = x;
System.out.println("Narrow Type Casting !");
}
}
You can also try this code with Online Java Compiler
Even though 'x' contains a value that lies in the integer range, the compiler will not do it on its own. It is showing "possible lossy conversion”.
We have now discussed long and int data types and type casting. Let's move on to the next step of converting long to int in Java.
Methods to Convert Long to Int in Java
Following are the three methods to convert long to int in Java:
Explicit Type casting
When a programmer explicitly directs the compiler to convert a variable into a variable of a different data type. It is implemented by using '(newDataType)', as shown in the following example.
Using (int) before x while assigning it to y, converts it first to an integer data type.
Using toIntExact() Method
Conversion of long to int in Java is possible using toIntExact() function of the Math library. Note: It will convert only if the value of the long variable lies in the integer range.
Syntax
int result = Math.toIntExact(longValue);
Example
public class MyClass {
public static void main(String args[]) {
long x = (long) Math.pow(2,33);
int y = Math.toIntExact(x);
System.out.println("Using toIntExact !");
}
}
You can also try this code with Online Java Compiler
In this example, ‘x’ contains a value that is outside the range of an integer. Hence, It is showing an “integer overflow” error.
Below is the corrected code:
public class MyClass {
public static void main(String args[]) {
long x = 555L;
int y = Math.toIntExact(x);
System.out.println("Using toIntExact !");
System.out.println("Value of y is "+y);
}
}
You can also try this code with Online Java Compiler
Instead of using primitive data type, we use an object of class Long, and then we can use this method to convert long to int in Java. The following code demonstrates the conversion:
Syntax
int result = numericObject.intValue();
Example
public class MyClass {
public static void main(String args[]) {
Long x = 555L; // long wrapper class datatype
int y = x.intValue();
System.out.println("Using intValue !");
System.out.println("Value of y is "+y);
}
}
You can also try this code with Online Java Compiler
Java is an object-oriented language. Its many features depend heavily on the concept of the object. That's why sometimes we need int, long, i.e. primitive datatypes in object form. Wrapper classes are just a method to get objects of such primitive data types.
What is signed representation?
In signed representation, the most significant bit denotes the sign of the number, i.e. positive or negative. The rest bits (except the most significant bit) make the magnitude of the number. For example, In signed representation, 1111 is -7, and 0111 is +7.
How do we represent a negative number in two’s complement representation?
To represent a negative number in two’s complement notation, reverse each bit of the number and then add 1 to it. That will make the negative number in two’s complement.
How do you convert a long string to an integer?
To convert a long string to an integer in programming, parse the string using functions like int() in Python, Integer.parseInt() in Java, or atoi() in C, ensuring the string represents a valid integer.
Conclusion
We use primitive data types such as integers and long to store numbers. Integer can store data ranging from -231 to 231-1, while long stores data between -263 and -263 +1. Sometimes, we need to assign the value of a long variable to the int one. This is called narrow-type casting. In this article, we have discussed several methods for conversion of long to int in Java. Explicit type casting, toIntExact(), and intValue() in case of long wrapper class are discussed.