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

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example