Java – Convert Int to Double
In this tutorial, we shall learn how to convert an integer value to a double value.
Firstly, we shall go through the process of widening casting, where we shall take the advantage of implicit casting of lower datatype to higher datatypes. Secondly, we shall discuss about Integer.doubleValue() method, which returns this integer as a double.
1. Convert int to double using Widening Casting
Int is a smaller datatype and double is a larger datatype. So, when you assign an int value to double variable, the conversion of int to double automatically happens in Java. This is also called widening casting.
So, to convert an int to double using widening casting, you can just assign a value of int to double.
In the following example, we have a variable i
which is of int type. We shall assign i
to double variable d
during its declaration.
Java Program
/**
* Java Program - Convert Int to Double
*/
public class IntToDouble {
public static void main(String[] args) {
int i = 125;
double d = i;
System.out.println(d);
}
}
Output
125.0
2. Convert int to double using Integer.doubleValue()
Integer.doubleValue() performs a widening primitive conversion on this integer object and returns a double value.
In the following example, we shall take an integer object initialized to a value, and call doubleValue() method on this object.
Java Program
/**
* Java Program - Convert Int to Double
*/
public class IntToDouble {
public static void main(String[] args) {
Integer i = 85;
double d = i.doubleValue();
System.out.println(d);
}
}
Output
85.0
Conclusion
In this Java Tutorial, we learned how to write Java programs to convert an Int to Double, with the help of example Java programs.