Java Program to Convert int to long - The Coding Shala

Home >> Java Programs >> Convert int to long

 Hey there, welcome back to another post. In this post, we are going to learn how to convert int to long in Java.

Java Program to Convert int to long

We can convert int to long using the below ways:

  • When we convert from smaller data type to larger data type, the Java compiler does it automatically, knowns as implicit conversion.
  • Using Long Wrapper class's valueOf() method.
Java Program: 

/**
 * https://www.thecodingshala.com/
 */

public class Main {

    public static void main(String[] args) {
        int intNumber = 15;

        // Implicit type casting for smaller to larger data types
        long longNumber1 = intNumber;
        System.out.println("Implicit Converted long number is: " + longNumber1);

        // using Long wrapper class
        long longNumber2 = Long.valueOf(intNumber);
        System.out.println("Using Wrapper class converted long number is: " + longNumber2);
    }
}

Output: 

Implicit Converted long number is: 15
Using Wrapper class converted long number is: 15


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Introduction to Kotlin Programming Language for Backend Development - The Coding Shala