Signup/Sign In

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); 
	}
}


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);  
	}
}


200.0
200.0



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.