Java program to convert long type variables into int
sptth://www.theitroad.com
Here is an example Java program that demonstrates how to convert a long
variable to an int
variable:
public class LongToIntExample { public static void main(String[] args) { long longValue = 123456789L; int intValue = (int) longValue; System.out.println("Long value: " + longValue); System.out.println("Int value: " + intValue); } }
In this program, we create a long
variable longValue
and initialize it with the value 123456789L
. We then cast this value to an int
using the (int)
cast operator and assign the result to the variable intValue
.
Finally, we print the values of longValue
and intValue
to the console using the System.out.println()
method.
Note that when you cast a long
value to an int
, you may lose precision if the long
value is too large to be represented as an int
. In such cases, the result of the cast may not be the same as the original long
value.