Java Program to Convert Int to Long

This article shows how to write a Java program to convert integer or int to the long data type. We can use the assignment operator (=) to convert the lower (integer) to the higher data type (long). The equals operator implicitly converts the integer to long.

Java Program to Convert Int to Long

In the below Java example, we declared an integer variable and assigned 10. Next, we assigned an int value to a long variable.

public class IntToLong {

	public static void main(String[] args) {
		int i = 10;
		
		long l1 = i;
		
		System.out.println(l1);
	}
}

Convert Integer to Long output

10

Java Program to Convert Int to Long using valeuOf

The other option to convert int to Long is to initiate integer as long or use the Long.valueOf() function. Please refer to Programs in Java.

import java.util.Scanner;

public class IntToLong {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int i;
		sc= new Scanner(System.in);

		System.out.println("\n Please Enter any Integer Value :  ");
		i = sc.nextInt();
		
		long l1 = i;
		
		long l2 = new Long(i);
		
		long l3 = Long.valueOf(i);
		
		System.out.println("The first way to convert int to Long = " + l1);
		System.out.println("The second way to convert int to Long = " + l2);
		System.out.println("The third way to convert int to Long = " + l3);
	}
}
Java Program to Convert Int to Long