3 ways : Convert int to double in Java

In the last tutorial I have shared how to convert double to int in java. In this tutorial I will share three ways on how to convert int to double in java.

Double has longer range than int data type. Java implicitly converts int value to double without the need of typecasting.

Read Also :  Convert double to int in java

Three ways to Convert int to double in java are:

1. Convert int to double implicitly using assignment operator
2. Convert int to double using Double wrapper class valueOf() method
3. Convert int to double using Double wrapper class constructor

 1. Convert int to double implicitly using assignment operator

You do not have to do anything. Java compiler will automatically convert lower data type(int) to higher data type(double). Since higher data type has wider range and greater memory size than lower data type. It is called as implicit typecasting.

/* Java Program to demonstrate
   conversion of int to double
   using assignment operator */
   
   public class JavaHungry {
       
     // main method
     public static void main (String args[]) {
         
        // given int value
        int num = 99;
     
        // converting to double
        // using assignment operator
        double result = num;
     
        // showing the double value
        System.out.println(result);
       
     }
   }

Output : 99.0

2. Convert int to double using Double wrapper class valueOf() method

Double wrapper class has valueOf() method which is used to convert int to double.

/* Java Program to demonstrate
   conversion of int to double
   using Double.valueOf() method*/
   
   public class JavaHungry {
       
     // main method
     public static void main (String args[]) {
         
        // given int value
        int num = 99;
     
        // converting to double
        // using Double valueOf() 
        Double result = Double.valueOf(num);
     
        // showing the double value
        System.out.println(result);
       
     }
   }

Output : 99.0

3. Convert int to double using Double wrapper class constructor

Another way to convert int to double is to pass the int value to the constructor of the Double wrapper class.

/* Java Program to demonstrate
   conversion of int to double
   using Double constructor */
   
   public class JavaHungry {
       
     // main method
     public static void main (String args[]) {
         
        // given int value
        int num = 99;
     
        // converting to double
        // using Double wrapper class
        //constructor 
        Double result = new Double(num);
     
        // showing the double value
        System.out.println(result);
       
     }
   }

Output : 99.0

In this short tutorial I have demonstrated 3 ways to convert int to double in java.Please mention in the comments if you have any questions.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry