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
๐ŸŒ
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); } }...
๐ŸŒ
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.
๐ŸŒ
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
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 โ€บ home โ€บ java/lang โ€บ java integer longvalue method
Java Integer longValue Method
September 1, 2008 - Learn how to use the longValue method of the Integer class in Java to convert an Integer to a long data type with examples.
๐ŸŒ
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 ...
๐ŸŒ
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....
๐ŸŒ
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 ...
๐ŸŒ
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:
๐ŸŒ
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.
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ java โ€บ programs โ€บ convert-int-to-long
Convert integer to long in Java (6 Programs)
October 18, 2025 - Learn how to convert int type variables to long in Java with simple examples. Explore implicit conversion, explicit casting, Long.valueOf and more. Read now!
๐ŸŒ
TheServerSide
theserverside.com โ€บ blog โ€บ Coffee-Talk-Java-News-Stories-and-Opinions โ€บ String-to-long-in-Java
String to long in Java
Integer has parseInt(String text). Short has parseShort(String text). Byte has parseByte(String text). Each of these methods return a whole number that fits within the range of a long in Java. You can use them to convert a String to a long. The following example uses the Integer wrapper classโ€™ parse method to convert a text String to a long: