In this java tutorial, we will see how to convert int to double in Java. Since double has longer range than int data type, java automatically converts int value to double when the int value is assigned to double.
1. Java implicit conversion from int to double without typecasting.
2. Java – Convert int to double using Double
wrapper class.
1. Java implicit conversion from int to double without typecasting
Since double data type has wider range and greater memory size than int, the conversion from int to double is implicit. As you can see we have not done the typecasting like we did in double to int conversion in Java.
public class JavaExample{ public static void main(String args[]){ //int value int inum = 101; //int to double conversion without typecasting double dnum = inum; //displaying double value System.out.println(dnum); } }
Output:
2. Java – Convert int to double using Double wrapper class
In this example we are doing the int to double conversion using the valueOf() method of Double wrapper class. This method accepts the other type value as parameter and returns the same value converted in double type.
public class JavaExample{ public static void main(String args[]){ //int value int inum = 55; /* valueOf() method of Double class converts the passed * value to the double value. */ Double dnum = Double.valueOf(inum); //displaying output System.out.println(dnum); } }
Output:
Leave a Reply