Integer i = theLong != null ? theLong.intValue() : null;

or if you don't need to worry about null:

// auto-unboxing does not go from Long to int directly, so
Integer i = (int) (long) theLong;

And in both situations, you might run into overflows (because a Long can store a wider range than an Integer).

Java 8 has a helper method that checks for overflow (you get an exception in that case):

Integer i = theLong == null ? null : Math.toIntExact(theLong);
Answer from Thilo on Stack Overflow
People also ask

What are the main methods to convert an Integer to a Long in Java?
A: The main methods are using Long.valueOf(), longValue() method, Long.parseLong(), autoboxing, and explicit casting.
🌐
code2care.org
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
What is autoboxing in the context of Integer to Long conversion?
A: Autoboxing is a Java feature that automatically converts primitive types to their corresponding wrapper classes. For Integer to Long conversion, it involves casting the int to long, then autoboxing to Long.
🌐
code2care.org
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
Which method is the most efficient for Integer to Long conversion?
A: For most cases, Long.valueOf() is efficient due to its caching mechanism. However, for high-performance scenarios, direct casting (long) with autoboxing might be slightly faster.
🌐
code2care.org
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
🌐
W3Docs
w3docs.com › java
Converting Integer to Long
This allows you to use the same ... Long l = n.longValue(); ... You can also use the longValueExact() method of the Integer class to convert an Integer object to a Long object....
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-int-to-long
Java Program to Convert int to long - GeeksforGeeks
July 23, 2025 - Since integer is a smaller data type compared to long, the compiler automatically converts int to long, this is known as Implicit type casting or type promotion. ... // Java program to demonstrate // use of implicit type casting import java.io.*; ...
🌐
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.
🌐
BeginnersBook
beginnersbook.com › 2019 › 04 › java-int-to-long-conversion
Java Convert int to long with examples
In the following example, we are converting int to long using valueOf() method of Long Wrapper class. The valueOf() method accepts integer as an argument and returns a long value after the conversion. public class JavaExample{ public static void main(String args[]){ int inum = 99; Long lnum ...
🌐
Tutorial Gateway
tutorialgateway.org › java-program-to-convert-int-to-long
Java Program to Convert Int to Long
January 22, 2025 - 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); } }
Find elsewhere
🌐
Coderanch
coderanch.com › t › 395715 › java › converting-int-long
converting int to long (Beginning Java forum at Coderanch)
To convert long to int :- total =total+Integer.parseInt(String.valueOf(text)); To convert int to long :- text =text+Long.parseLong(String.valueOf(total)); regards, lalit [ February 26, 2004: Message edited by: Lalit K Kumar ] This seems like a very convoluted way to convert from one numeric ...
🌐
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 ...
🌐
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 ...
🌐
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 - Generally, type casting and data type conversion in Java are efficient operations. However, excessive and unnecessary conversions can impact performance. It’s essential to convert data types only when required. Yes, you can convert a long back to an int using type casting. However, be aware that if the long value is outside the int range, data loss may occur. In some cases, you may consider using BigInteger for arbitrary-precision integers or handle data type conversions through custom logic.
🌐
Mkyong
mkyong.com › home › java › java – convert integer to long
Java - Convert Integer to Long - Mkyong.com
August 1, 2019 - package com.mkyong.example; public class TestInteger { public static void main(String[] args) { Integer num = 1; System.out.println(num); // 1 Long numInLong = Long.valueOf(num); System.out.println(numInLong); // 1 } } Long.valueOf JavaDocs) Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities. 1 Comment · Most Voted · Newest Oldest · Inline Feedbacks · View all comments · Coder · 4 years ago · Its not working. 2 · Reply · How to convert negative number to positive in Java ·
🌐
Programiz
programiz.com › java-programming › examples › int-to-long-conversion
Java Program to convert int type variables to long
Become a certified Java programmer. Try Programiz PRO! ... 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 } }
🌐
LabEx
labex.io › tutorials › java-java-integer-longvalue-method-117718
Java Data Type Conversion | Integer to Long | LabEx
Integer integerObject = ... the following command to compile the Java file: ... This lab demonstrated how to use the longValue() method to convert an Integer object to its long equivalent....
🌐
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 ...
🌐
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!
🌐
PREP INSTA
prepinsta.com › home › java tutorial › java program to convert int to long
Java Program to Convert int to long | Prepinsta
May 26, 2023 - 2. Using the "L" suffix: If you are assigning an integer literal to a long variable, you can use the "L" suffix to indicate that the literal should be treated as a long. ... 3. Using valueOf() method: You can also use the "valueOf()" method ...