To convert int to long in Java, we just need to assign the int value to long. That is called implicit type casting since we are converting the smaller data type to a bigger one.
Example
class Test { public static void main(String[] args) { int num = 302; long l = num; System.out.println(l); } }
Output: 302
We can also convert int to long using the Long.valueOf() method which accepts int as an argument and returns a long.
Example
class Test { public static void main(String[] args) { int num = 302; long l = Long.valueOf(num); System.out.println(l); } }
Output: 302
That’s all about how to convert int to long in Java. In the next lesson, we will cover converting long to int.
Happy coding!