Java Program to Convert int to double - The Coding Shala

Home >> Java Programs >> Convert int to double

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

Java Program to Convert int to double

As we know, double is a larger data type than int so the Java compiler automatically converts int to double when we assign an int to double data type. The most common ways are below to convert int to double:

  1. Implicit type conversion.
  2. By using the Double Wrapper class's valueOf() method.

Now let's see the above methods in detail with examples in Java.

Java Example to Convert int to double by Implicit Conversion

The Java compiler will automatically convert smaller data types to larger data types.

Java Program: 

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

public class Main {

    public static void main(String[] args) {
        int num = 50;

        // implicit type conversion
        double dNum = num;
        System.out.println("int is: " + num);
        System.out.println("Converted double is: " + dNum);
    }
}

Output: 

int is: 50
Converted double is: 50.0

Java Example to Convert int to double by Using Double.valueOf() method

We can use the Double Wrapper class's valueOf() method to convert int to double data type.

Java Program: 

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

public class Main {

    public static void main(String[] args) {
        int num = 50;

        // by using Double.valueOf()
        double dNum = Double.valueOf(num);
        System.out.println("int is: " + num);
        System.out.println("Converted double is: " + dNum);
    }
}

Output: 

int is: 50
Converted double is: 50.0


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

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala