int
) variables into the double in Java.class Main {
public static void main(String[] args) {
// create int variables
int a =33;
int b = 29;
// convert int into double
// using typecasting
double c = a;
double d = b;
System.out.println(c); // 33.0
System.out.println(d); // 29.0
}
}
33.0
29.0
In the above example, we have int
type variables a
and b
. Notice the line,
double c = a;
Here, the int
type variable is automatically converted into double
. It is because double
is a higher data type (data type with larger size) and int
is a lower data type (data type with smaller size).
Hence, there will be no loss in data while converting from int
to double
.
int
type variable into an object of the Double
class. For example,class Main {
public static void main(String[] args) {
// create int variables
int a = 332;
// convert to an object of Double
// using valueOf()
Double obj = Double.valueOf(a);
System.out.println(obj); // 332.0
}
}
332.0
Double.valueOf()
method to convert the variable a into an object of Double
.Double
is a wrapper class in Java.