In this guide, we will see how to convert long to int with examples. Since long is larger data type than int, we need to explicitly perform type casting for the conversion.
Java long to int example
In the following example we are converting long data type to int data type using explicit typecasting. Here we have a variable lnum
of long data type and we are converting the value of it into an int value which we are storing in the variable inum
of int data type.
public class JavaExample{ public static void main(String args[]){ long lnum = 1000; //type casting - long to int int inum = (int)lnum; System.out.println("Converted int value is: "+inum); } }
Output:
Java long to int example using intValue() method of Long wrapper class
In the following example, we have a Long object lnum
and we are converting it into an int primitive type using intValue()
method of Long class.
public class JavaExample{ public static void main(String args[]){ //Long object Long lnum = 99L; //Converting Long object to int primitive type int inum = lnum.intValue(); System.out.println("Converted int value: "+inum); } }
Output:
Leave a Reply