Menu

How to Convert int to double in Java?

How to convert Java int to double

In Java, an int value can be converted into a double value by using the simple assignment operator. This is because this conversion is an example of Type Promotion(Implicit type conversion) in which a lower data type is automatically converted into a higher data type.

Example 1:

Here, the int values are converted into double values implicitly by using the assignment operator.

public class StudyTonight
{  
    public static void main(String args[])
    {  
        int i = 500;  
        double l = i;  
        System.out.println(" The double value is : " +l); 
    }
}

Output:

The double value is : 500.0

Also, we can convert an int value into a Double Object either by instantiating Double class or by using the Double.valueOf() method.

Example 2:

Here, the int value is converted into the double by instantiating the class and using valueOf() method.

public class StudyTonight
{  
    public static void main(String args[])
    {  
        int i = 200;  
        double d1 = new Double(i);  //conversion using the instantiation  
        double d2 = Double.valueOf(i); //conversion using the valueOf() method  
        System.out.println(d1);  
        System.out.println(d2);  
    }
}

Output:

200.0 200.0