How to convert long to int in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        long l = 93732;
        
        int n = Math.toIntExact(l);
        
        System.out.println(n);
    }
}




/*
run:

93732

*/

 



answered Sep 16, 2020 by avibootz
0 votes
class Main {
    public static void main(String[] args) {
        long bigNumber = 123458901L;
        int intNumber = (int)bigNumber; // May cause overflow
        
        System.out.println(intNumber);

    }
}


/*
run:

123458901

*/

 



answered May 18, 2025 by avibootz

Related questions

...