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 Java assignment operator (=) to convert lower data type (integer) to higher data type (long). The equals operator implicitly converts the integer to long.

In the below Java example, we declared an integer variable and assigned 10. Next, we assigned 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);
	}
}

Java Convert Integer to Long output

10

Java Program to Convert Int to Long example 2

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

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 2