Java Program to Convert long into int

Java Program to convert long into int

What is int in Java ?

“int” is a primitive data type in Java that is used to represent integers (whole numbers) within a certain range of values. The range of values that can be stored by an “int” is from -2^31 to 2^31-1. The “int” data type is commonly used in mathematical operations and to represent arrays of integers.

What is long in Java ?

In Java, “long” is a primitive data type used to represent integer values that are larger than the range of “int” data type.
The “long” data type is a 64-bit two’s complement integer, which means it can hold values between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 inclusive. The keyword to declare a variable of type “long” is simply “long”.

Ways to Convert long to int  : 

Syntax :

long longValue = 1000000000L;
int intValue = (int) longValue;

Note : If the value of longValue is outside the range of int values, the result of the cast operation may be unexpected or even incorrect.

Syntax :

long longValue = 1000000000L;
int intValue = Math.toIntExact(longValue);

Note : This method is useful when you want to ensure that the conversion from long to int will not result in a loss of information.

Syntax :

long longValue = 1000000000L;
int intValue = (int) (longValue & 0xffffffffL);

Note : This method is useful when you want to truncate the long value to fit into the range of int values, but note that it can result in the loss of information.

Example 1 : Using a cast operator  

Run

public class Main {
    public static void main(String[] args) {
        long longValue = 1234567890L;
        int intValue = (int) longValue;
        System.out.println("Long value: " + longValue);
        System.out.println("Int value: " + intValue);
    }
}

Output :

Long value: 1234567890
Int value: 1234567890

Example 2 : Using the Math.toIntExact() method

Run

public class Main {
    public static void main(String[] args) {
        long longValue = 1234567890L;
        int intValue = Math.toIntExact(longValue);
        System.out.println("Long value: " + longValue);
        System.out.println("Int value: " + intValue);
    }
}

Output :

Long value: 1234567890
Int value: 1234567890

Example 3 : Using bit masking

Run
public class Main {
    public static void main(String[] args) {
        long longValue = 1234567890L;
        int intValue = (int) (longValue & 0xffffffffL);
        System.out.println("Long value: " + longValue);
        System.out.println("Int value: " + intValue);
    }
}

Output :

Long value: 1234567890
Int value: 1234567890

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription