Converting an integer to a double is a common operation in Java programming.
This conversion can be achieved using a simple type-casting operation.
In this tutorial, we will discuss how to convert int type variables to double in Java using code examples.
To convert an int type variable to double, we can use the following code snippet:
int num = 10; double doubleNum = (double) num;
In the above code, we first declare an integer variable ‘num’ and initialize it with the value 10.
Then, we create a double variable ‘doubleNum’ and assign it the value of ‘num’ by performing a type-casting operation.
The type-casting operation is performed by enclosing the ‘num’ variable inside parentheses and then preceding it with the double data type.
Let’s take a look at another example:
int num1 = 20; int num2 = 5; double result = (double) num1 / num2;
In the above code, we first declare two integer variables ‘num1’ and ‘num2’ and initialize them with the values 20 and 5 respectively.
Then, we create a double variable ‘result’ and assign it the value of ‘num1’ divided by ‘num2’.
The type-casting operation is performed on ‘num1’, which converts it to a double before performing the division operation.
In conclusion, converting an int type variable to double in Java is a simple task that can be accomplished using the type-casting operator.
By performing a type-casting operation, we can easily convert an integer to a double and perform mathematical operations that require a double data type.




