Java Program to Convert Int to Double
Last Updated :
21 Jan, 2025
Java data types can be categorized as primitive and non-primitive. Primitive data types contain a single value, whereas non-primitive data types contain an address of the variable value. Java supports 7 primitive data types - boolean, byte, char, short, int, long, float, and double. These data types require a different amount of space while stored in memory.
Data Type | Bits Acquired in Memory |
---|
boolean | 1 |
byte | 8 |
char | 16 |
short | 16 |
int | 32 |
long | 64 |
float | 32 |
double | 64 |
Int stores 32-bit signed two’s complement integer and require 32 bits (4 bytes) to be stored in memory while double stores double-precision 64-bit floating-point numbers and require 64 bits (8 bytes) for storage. Many times int values need to be converted to double so, to perform such conversion following three methods can be used:
- Conversion using assignment operator or Implicit Conversion
- Conversion using Double class constructor
- Conversion using valueOf() method
1. Conversion using an Assignment Operator
Java performs implicit conversion or widening conversion when both data types involved in conversion are compatible, or the value of a smaller data type is assigned to a bigger data type. Double is bigger than int as it requires more storage space; hence, int values are implicitly converted to double by Java.
Syntax:
double d = i
Here,
- d = variable to store double value after conversion to double data type
- i = integer value to be converted to double data type
Java
// Java program to convert int to
// double using assignment operator
class GFG {
public static void main(String[] args)
{
int i = 100;
// Implicit conversion from int to double data type
double d = i;
System.out.println("Integer value " + i);
System.out.println("Double value " + d);
}
}
OutputInteger value 100
Double value 100.0
2. Conversion using Double Wrapper Class Constructor
Double class in Java is a wrapper class used to create objects that can hold single, double type values and contain several methods to deal with double matters. Int value can be passed to the constructor of the Double class to create an object of double type initialized with provided integer value.
Syntax:
Double d = new Double(i)
Here,
- d = variable to store double value after conversion to double data type
- i = integer value to be converted to double data type
Java
// Java program to convert int to double
// using Double wrapper class constructor
class GFG {
public static void main(String[] args)
{
int i = 100;
// Conversion of int to double data
//type using Double wrapper class
Double d = new Double(i);
System.out.println("Integer value " + i);
System.out.println("Double value " + d);
}
}
Output
Integer value 100
Double value 100.0
3. Conversion using valueOf() Method of Double Wrapper Class
Double wrapper class valueOf() method converts int value to double type. It returns the Double-object initialized with the provided integer value.
Syntax:
Double d = Double.valueOf(i)
Here,
- d = variable to store double value after conversion to double data type
- i = integer value to be converted to double data type
Java
// Java program to convert int
// to double using valueOf() method
class GFG {
public static void main(String[] args)
{
int i = 100;
// Conversion of int to double data type using valueOf() method
Double d = Double.valueOf(i);
System.out.println("Integer value " + i);
System.out.println("Double value " + d);
}
}
OutputInteger value 100
Double value 100.0
Example to Convert int[] array to a double[] array
To convert an int[] array to double[] array , we can iterate through the int array and cast each value to double.
Java
// Java program to convert
// int[] array into double[] array
public class Geeks {
public static void main(String[] args) {
// Initialize the int array
int[] arr = { 1, 2, 3, 4, 5 };
// Convert int array to double array
// using toDoubleArray method
double[] darr = toDoubleArray(arr);
for (double d : darr) {
System.out.print(d + " ");
}
}
// Method to convert int array to double array
public static double[] toDoubleArray(int[] arr)
{
// Create a new double array with the
// same length as the int array
double[] darr = new double[arr.length];
// Loop through the int array and
// convert each element to double
for (int i = 0; i < arr.length; i++) {
// Cast int element to double
darr[i] = (double)arr[i];
}
return darr;
}
}
Output1.0 2.0 3.0 4.0 5.0
Similar Reads
Java Program to Convert Double to Long Given a Double real number. Write a Java program to convert the given double number into a Long (long) in Java. Examples: Input: double = 2545.241 Output: 2545 Input: double = 21.54 Output: 21 Double data type: The double data type is a double-precision 64-bit IEEE 754 floating-point. Its value rang
4 min read
Java Program to Convert Double to String The primary goal of double to String conversion in Java is to store big streams of numbers that are coming where even data types fail to store the stream of numbers. It is generically carried out when we want to display the bigger values. In this article, we will learn How to Convert double to Strin
3 min read
Java Program to Convert int to long Given a number of int datatype in Java, your task is to write a Java program to convert this given int datatype into a long datatype. Example: Input: intnum = 5 Output: longnum = 5 Input: intnum = 56 Output: longnum = 56 Int is a smaller data type than long. Int is a 32-bit integer while long is a 6
2 min read
Java Program to Convert Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s
4 min read
Java Program to Convert long to int Long is a larger data type than int, we need to explicitly perform typecasting for the conversion. Typecasting is performed through the typecast operator. There are basically three methods to convert long to int: By type-castingUsing toIntExact() methodUsing intValue() method of the Long wrapper cla
3 min read
Java Program to Convert Octal to Decimal Octal numbers are numbers with 8 bases and use digits from 0-7. This system is a base 8 number system. The decimal numbers are the numbers with 10 as their base and use digits from 0-9 to represent the decimal number. They also require dots to represent decimal fractions.We have to convert a number
3 min read
Java Program to Convert a Float value to String Given a Float value in Java, the task is to write a Java program to convert this float value to string type. Examples: Input: 1.0 Output: "1.0" Input: 3.14 Output: "3.14" Strings - Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are
4 min read
Java Program to Convert Fahrenheit into Celsius In this article, we will discuss how to convert a temperature value from Fahrenheit to Celsius using Java.Note: Fahrenheit and Celsius are units of temperature measurement, denoted by °F and °C, respectively.FormulaCelsius = (Fahrenheit - 32) / 1.8ApproachInitialize the value of Fahrenheit as 50.0 a
1 min read
Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System
5 min read
Convert String to Float in Java To convert a String to a Float in Java, there are several ways including built-in methods and handling potential exceptions for invalid inputs.Example: For this conversion, we will use the most common and efficient method i.e. parseFloat() method. This method converts a string into a primitive float
2 min read