
There are multiple ways in which you can convert a primitive int or an Integer object to a Long object,
Example 1: Using the Long constructorConstructs a newly allocated Long object that represents the specified long argument.
public static void main(String[] args) {
Integer i = 1000;
Long l = new Long(integer);
System.out.println(l);
}
Output: 1000
Note: It is better to avoid using this constructor as it has been deprecated as of Java 9, as it's rarely appropriate to use this constructor. The Java doc for this constructor says "The static factory valueOf(long) is generally a better choice, as it is likely to yield significantly better space and time performance."
Example 2: Using Long.valueOf(Long l)
This method is available since Java 1.5. It returns a Long instance representing the specified long (int goes through an implicit type casing) value.
public static void main(String[] args) {
Integer integer = 2000;
Long long = Long.valueOf(integer);
}
⚡️ Long.valueOf method caches values in the range -128 to 127, inclusive, and may cache other values outside of this range.
Example 3: A little more..
public static void main(String[] args) {
Integer integer = 3000;
Long long = Long.valueOf(integer.longValue());
}
- [Fix] java.time.zone.ZoneRulesException: Unknown time-zone ID
- Parse XML file in Java using DOM Parser
- Java equals method - Tutorial
- [Program] How to read three different values using Scanner in Java
- Java: The value of the local variable string is not used
- Display Output in Java Console as a Table
- How to detect Operating System using Java code
- Java 8 Streams map() with examples
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Add newline character Java code example (\r \n \r\n)
- List of Java Major Minor Version Numbers
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Java - Check if array contains the value
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- XmlRpcException ConnectException connection refused error
- Create a Zip file using Java Code programmatically
- List of jar files for Jax-ws (SOAP) based Java Web Services
- How to fix Java HTTP java.net.UnknownHostException
- List of jars required for Struts2 project
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- Get the current timestamp in Java
- java: unclosed string literal [Error]
- Command to display epoch time Terminal - Linux
- SharePoint 2010 DataForm Unable to display this Web Part System.StackOverflowException - SharePoint
- 'Edit Document' Requires a Windows Sharepoint Services-compatible application and Microsoft Internet Explorer 6.0 or higher - SharePoint
- zsh: command not found: nvm [brew macOS] - MacOS
- How to know installed version of Homebrew - MacOS
- Notepad++ Reload - This file has been modified by another program. Do you want to reload it? - NotepadPlusPlus
- How to add to PATH in macOS Big Sur - MacOS
- Add Text at Start and End of Each Line Notepad++ - NotepadPlusPlus