Note that there is a difference between a cast to long and a cast to Long. If you cast to long (a primitive value) then it should be automatically boxed to a Long (the reference type that wraps it).

You could alternatively use new to create an instance of Long, initializing it with the int value.

Answer from Daniel Earwicker on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-convert-int-to-long
Java Program to Convert int to long - GeeksforGeeks
July 23, 2025 - Using valueOf() method of the Long wrapper class in java which converts int to long.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2019 โ€บ 04 โ€บ java-int-to-long-conversion
Java Convert int to long with examples
Since integer is a smaller data ... public class JavaExample{ public static void main(String args[]){ int num = 10; /* Implicit type casting, automatic * type conversion by compiler */ long lnum = num; System.out.println(lnum); } }...
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ int-to-long-conversion
Java Program to convert int type variables to long
... class Main { public static void main(String[] args) { // create int variables int a = 25; int b = 34; // convert int into long // using typecasting long c = a; long d = b; System.out.println(c); // 25 System.out.println(d); // 34 } } In the above example, we have int type variables a and b.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ convert-int-type-variables-to-long
Java Program to convert int type variables to long | Vultr Docs
November 26, 2024 - Recognize that Java supports automatic type conversion from int to long. Assign an int value directly to a long variable. ... int myInt = 10000; long myLong = myInt; System.out.println("The converted long value is: " + myLong); Explain Code
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java numbers โ€บ convert int to long in java
Convert int to Long in Java | Baeldung
May 14, 2024 - The (long) before intTen is a type-casting operator that tells the compiler to convert intTen to long before assigning it to longTen. This process is automatic, hence the term autoboxing. The Long class in Java is a wrapper class that provides several utility methods for dealing with long values.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ convert int to long in java
Convert int to Long in Java - Scaler Topics
April 7, 2024 - ... The (long) expression before the int variables (i), (j), and (k) in the above Java program converts the value of i = 10, j = 15, and k = 20 to long type, which is the explicit type casting method.
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-int-to-long
Java Convert int to long
Java Convert int to long example and examples of string to int, int to string, string to date, date to string, string to long, long to string, string to char, char to string, int to long, long to int etc.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ java-program-to-convert-int-to-long
Java Program to Convert Int to Long
January 22, 2025 - 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); ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 395715 โ€บ java โ€บ converting-int-long
converting int to long (Beginning Java forum at Coderanch)
You should use the cast operators instead: To convert from int to long: text = text + (long)total; To convert from long to int: total = total + (int)text; Another solution is to just declare both variables as the same type.
๐ŸŒ
W3Docs
w3docs.com โ€บ java
Converting Integer to Long
This allows you to use the same method to convert any object that implements the Number interface to a long, including Byte, Short, Integer, and Long objects: Number n = new Integer(123); Long l = n.longValue(); ... You can also use the ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ lang โ€บ integer_longvalue.htm
Java - Integer longValue() method
Following is the declaration for java.lang.Integer.longValue() method ... This method returns the numeric value represented by this object after conversion to type long. ... The following example shows the usage of Integer longValue() method to get long value from an Integer object.
๐ŸŒ
Java67
java67.com โ€บ 2017 โ€บ 09 โ€บ how-to-convert-primitive-int-value-to-Long-object-java.html
How to convert int value to a Long object in Java? Examples | Java67
In our case, it is still ok because ... of the better ways to convert an int primitive to the Long object is by using the valueOf() method of the Long class....
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-int-to-long
Convert Int to Long in Java
January 9, 2023 - /** * Java Program - Convert Int to Long */ public class IntToLong { public static void main(String[] args) { Integer i = 125; long l = 125; System.out.println(l); } } ... Integer.longValue() performs a widening primitive conversion on this ...
๐ŸŒ
Scientech Easy
scientecheasy.com โ€บ home โ€บ blog โ€บ how to convert int to long in java
How to Convert Int to Long in Java - Scientech Easy
June 11, 2025 - Letโ€™s make a simple Java program for converting int to Long object using valueOf() method of Long class.
๐ŸŒ
Code2care
code2care.org โ€บ home โ€บ java โ€บ ways to convert integer or int to long in java
Ways to Convert Integer or int to Long in Java | Code2care
September 20, 2024 - Constructs a newly allocated Long object that represents the specified long argument. public static void main(String[] args) { Integer i = 1000; Long l = new Long(i); System.out.println(l); } ... Note: It is better to avoid using this constructor ...
๐ŸŒ
Apps Developer Blog
appsdeveloperblog.com โ€บ home โ€บ java โ€บ java conversion โ€บ convert int to long in java
Convert Int to Long in Java - Apps Developer Blog
March 22, 2024 - The widening casting sequence provides flexibility in Java programming, allowing you to work with different data types while preserving the integrity of the values. Hereโ€™s an example of implicit casting from int to long: ... In this example, the value of myInt is implicitly cast to long during the assignment to myLong. No explicit casting is required. Explicit casting, also known as narrowing casting, is used when you need to convert ...
๐ŸŒ
SourceBae
sourcebae.com โ€บ home โ€บ how to convert int to long in java: a comprehensive guide
How to Convert int to long in Java: A Comprehensive Guide - SourceBae
August 21, 2025 - Type casting is a common method for converting between data types in Java. To convert an int to long, you can use a simple cast: ... In this example, we cast the int variable myInt to a long.
๐ŸŒ
Sarthaks eConnect
sarthaks.com โ€บ 3508089 โ€บ explain-java-convert-int-to-long
Explain Java Convert int to long - Sarthaks eConnect | Largest Online Education Community
LIVE Course for free ยท In Java, converting an int to a long is a straightforward process. Since a long can hold a wider range of values than an int, the conversion is implicit and does not result in any loss of data. Here's an example code snippet to demonstrate the conversion: