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
September 11, 2022 - Since integer is a smaller data type compared to long, the compiler automatically converts int to long, this is known as type promotion. public class JavaExample{ public static void main(String args[]){ int num = 10; /* Implicit type casting, automatic * type conversion by compiler */ long ...
Discussions

How can I convert a long to int in Java? - TestMu AI Community
I have a long value in Java that I want to convert to an int. What’s the correct and safe way to handle long to int java conversion, especially if the long might exceed the int range? More on community.testmu.ai
🌐 community.testmu.ai
0
June 18, 2025
Are Long better than Integer as keys for a Map?
He doesn't explain why a Long key is better than an Integer key, and I think the safest thing for now is to simply ignore this advise. If you originally had Integer keys and felt compelled to switch to Long keys, it won't improve anything. The xor with the upper bits is an xor of zero, which doesn't do anything. If you were generating random keys, there's no reason to choose Long over Integer, unless you need to store many billions of keys. The Java hashCode method returns an int, and so the xor of 32 random bits with 32 random bits doesn't increase randomness or hash spread. More on reddit.com
🌐 r/java
58
62
January 2, 2023
(Java) Loss of precision going from long to int.
Array indices can't be larger than int. And it wouldn't be efficient to use a long, anyway. In Java 7, we only have signed ints, which means our int has a maximum value of 2,147,483,647. Now assuming that Memory is an int (it's probably much larger), exceeding the limits of using ints for indices would mean greater than 8 GB of memory! If you need to be able to address this much "memory", an array is probably not going to work. A dictionary might be a better choice. EDIT: Also, Google works fine if you know what to look for. In this case, the issue is the fact that array indices are integers and you lose precision if we convert our long into an integer. Thus, we'd have to search for something related to "java array indice long". A sample google result . More on reddit.com
🌐 r/learnprogramming
8
12
December 2, 2013
converting ch to int java
int int1 = (int)ch; should do it, I don't know what part of your code isn't working like it's supposed to you could also try - int a=Character.getNumericValue(ch); More on reddit.com
🌐 r/CodingHelp
4
3
November 30, 2021
🌐
Scaler
scaler.com › home › topics › convert int to long in java
Convert int to Long in Java - Scaler Topics
April 7, 2024 - We can also convert an int variable to a long variable using explicit typecasting. We have to put the data type we want and the other variable/expression in the parentheses like the given syntax below: ... It creates a long data type value var ...
🌐
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.
🌐
W3Docs
w3docs.com › java
Converting Integer to Long
To convert an Integer object to a Long object in Java, you can use the longValue() method of the Integer class, which returns the value of the Integer object as a long.
🌐
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 - Converting int to long in Java is a straightforward process thanks to Java's built-in type conversion mechanisms. You can utilize implicit conversions in most scenarios for seamless coding. However, for enhanced code readability and when mixed type calculations are involved, using explicit casting or long literals is preferable.
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › java numbers › how to convert long to int in java
How to Convert Long to Int in Java
December 25, 2024 - So we don’t have the problem of discarded bits, as in the case of classic Java long to int conversion. The BigDecimal class in Java provides a reliable way to handle large numbers and ensures precision during conversions. While it is typically used for arithmetic operations with floating-point numbers, it can also be employed to safely convert a long to an int.
🌐
Coderanch
coderanch.com › t › 395715 › java › converting-int-long
converting int to long (Beginning Java forum at Coderanch)
February 26, 2004 - 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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-int-to-long
Java Program to Convert int to long - GeeksforGeeks
July 23, 2025 - Using a simple assignment. This is known as implicit type casting or type promotion, the compiler automatically converts smaller data types to larger data types. Using valueOf() method of the Long wrapper class in java which converts int to long.
🌐
Vultr Docs
docs.vultr.com › java › examples › convert-long-type-variables-into-int
Java Program to convert long type variables into int | Vultr Docs
December 17, 2024 - The conversion of long type variables to int in Java is a common task that developers encounter. This typically involves casting the longer 64-bit long data type into the shorter 32-bit int type. Such transformations are necessary when interfacing with libraries or APIs that only accept integers, or when memory constraints and structures require smaller data types. In this article, you will learn how to effectively convert ...
🌐
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!
🌐
Verve AI
vervecopilot.com › interview-questions › what-critical-insights-does-java-int-to-long-offer-for-your-next-interview
What Critical Insights Does `Java Int To Long` Offer For Your Next Interview?
August 15, 2025 - The need for java int to long conversion arises when you need to store an integer value that might exceed the int's maximum capacity, or simply when an operation requires a long type. Interviewers frequently probe this topic to assess your foundational knowledge of Java's type system, memory management, and data integrity principles. It demonstrates whether you understand the implications of data size and potential pitfalls in numerical operations. Converting an int to a long is a common operation in Java, primarily because it's a "widening primitive conversion," meaning you're moving from a smaller data type to a larger one.
🌐
Scaler
scaler.com › home › topics › convert java long to int
Convert Java long to int - Scaler Topics
April 19, 2024 - Long and int are both considered primitive data types in Java. Learn how to convert Java long to int using various methods along with their syntax and code examples on Scaler Topics.
🌐
W3Docs
w3docs.com › java
Convert Long into Integer
It is important to note that if ... and the resulting Integer will have a negative value. To avoid this, you can use the longValue() method of the Integer class, which returns the value of the Integer as a long....
🌐
W3Schools
w3schools.com › java › java_type_casting.asp
Java Type Casting
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Type casting means converting one data type into another. For example, turning an int into a double. ... Widening Casting (automatic) - converting a smaller type to a larger type size byte -> short -> char -> int -> long -> float -> double
🌐
TestMu AI Community
community.testmu.ai › ask a question
How can I convert a long to int in Java? - TestMu AI Community
June 18, 2025 - I have a long value in Java that I want to convert to an int. What’s the correct and safe way to handle long to int java conversion, especially if the long might exceed the int range?