To convert int to double in Java, we just need to assign the int value to a double. That is called implicit type casting since we convert a smaller data type to a bigger one.
Example
class Test {
public static void main(String[] args) {
int num = 128;
double d = num;
System.out.println(d);
}
}
Output: 128.0
We can also convert int to double using the Double.valueOf() static method which accepts int as an argument and returns a double.
Example
class Test {
public static void main(String[] args) {
int num = 128;
double d = Double.valueOf(num);
System.out.println(d);
}
}
Output: 128.0
That’s all about how to convert int to double in Java. Proceed to the next lesson.
Happy coding!