Examples
- How to Convert Char to String in Java? Character to String
- How to Convert Char to Int in Java? Character to Integer Conversion
- How to Convert Int to Char in Java? Integer to Character
- How to Convert Long to Int in Java? Long to Integer
- How to Convert Int to Long in Java? Integer to Long Program
- How to Convert Boolean to String in Java? (Parse Bool to String)
- How to Convert String to Boolean in Java? 3 Ways With Program
- How to Convert String to Int in Java? String to Integer Program
- How to Convert Int to String in Java? Integer to String Program
- How to Convert Int to Double in Java? (Integer to Double)
- How to Convert Double to Int in Java? 4 Ways
- How to Convert String to Double in Java? Programs
- How to Convert Double to String in Java? Programs
How to Convert Int to Double in Java? (Integer to Double)
In Java programming, it's common to encounter situations where you need to work with different data types. One such common task is to convert an integer (int) into a double (double). While Java is a strongly-typed language that typically requires explicit type conversions, the process of converting from int to double is straightforward once you understand the fundamentals.
In this tutorial, we'll walk you through the step-by-step process to convert int to double in Java. We'll explore various methods and techniques, ensuring you have a solid grasp of how to perform this conversion effectively.
Concepts to Learn for This Program:
Convert Integer to Double in Java Using Assignment Operator
Here's the Java program to convert int to double using the assignment operator, along with the explanation and expected output:
Code
public class IntToDoubleConversion {
public static void main(String[] args) {
int intValue = 42;
double doubleValue = intValue; // Using assignment operator to convert int to double
System.out.println("Original int value: " + intValue);
System.out.println("Converted double value: " + doubleValue);
}
}
Output
Original int value: 42
Converted double value: 42.0
Explanation
-
We declare an int variable intValue and initialize it with the value 42.
-
We declare a double variable doubleValue and assign the intValue to it. In Java, this is an example of implicit type casting or widening conversion, where the int value is automatically converted to a double because a double can hold a wider range of values than an int.
-
We use System.out.println to display the original int value and the converted double value.
As you can see, the original int value 42 is converted to a double with the value 42.0. Java performs this conversion automatically because a double can represent decimal values, while an int can only represent whole numbers.
Java Int to Double Using Double(int)
You can convert an int to a double in Java using a wrapper class constructor, specifically Double(int).
Code
public class IntToDoubleConversion {
public static void main(String[] args) {
int intValue = 42;
Double doubleValue = new Double(intValue); // Using wrapper class constructor
System.out.println("Original int value: " + intValue);
System.out.println("Converted double value: " + doubleValue);
}
}
Output
Original int value: 42
Converted double value: 42.0
Explanation
-
We declare an int variable intValue and initialize it with the value 42.
-
We create a Double object doubleValue by using the Double(int) constructor. This constructor takes an int as an argument and converts it to its double representation.
-
We use System.out.println to display the original int value and the converted double value.
Convert Int to Double in Java Using Double.valueOf()
You can also convert an int to a double in Java using the Double.valueOf(intValue) method.
Code
public class IntToDoubleConversion {
public static void main(String[] args) {
int intValue = 42;
double doubleValue = Double.valueOf(intValue); // Using Double.valueOf(intValue)
System.out.println("Original int value: " + intValue);
System.out.println("Converted double value: " + doubleValue);
}
}
Output
Original int value: 42
Converted double value: 42.0
Explanation
-
We declare an int variable intValue and initialize it with the value 42.
-
We use the Double.valueOf(intValue) method to convert the int value to its double representation. This method takes an int as an argument and returns a Double object representing the equivalent double value.
-
We assign the result to the doubleValue variable, which now holds the converted double value.
-
We use System.out.println to display the original int value and the converted double value.
Int to Double Conversion in Java Using Typecasting
In this method, we will convert an integer to double in Java using type casting.
Code
public class IntToDoubleConversion {
public static void main(String[] args) {
int intValue = 42;
double doubleValue = (double) intValue; // Using type casting
System.out.println("Original int value: " + intValue);
System.out.println("Converted double value: " + doubleValue);
}
}
Output
Original int value: 42
Converted double value: 42.0
Explanation
-
We declare an int variable intValue and initialize it with the value 42.
-
We use Java type casting to convert the int value to a double. The (double) before intValue explicitly tells Java to treat intValue as a double during the conversion.
-
We assign the result to the doubleValue variable, which now holds the converted double value.
-
We use System.out.println to display the original int value and the converted double value.
Learn Next: