Java – How to Convert Integer to Long

In this section, we will show you how to convert Integer to Long in Java.In Java, we can use Long.valueOf() to convert an Integer to a Long.

Integer i = 7; //example

Long l = Long.valueOf(i.longValue());
This avoids the performance hit of converting to a String. The longValue() method in Integer is just a cast of the int value. The Long.valueOf() method gives the vm a chance to use a cached value.

Main.java

public class Main {

public static void main(String[] args) {

Integer i = 7;
Long l = Long.valueOf(i.longValue());
System.out.println(l);

}
}

Console Output:
7

More topics,

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - DES Encryption and Decryption example

Java - Blowfish Encryption and decryption Example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Top 5 Java ORM tools - 2024

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String