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,

Popular posts from this blog

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

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

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

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

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