sample
double d = 5/10;
System.out.println(d);
The result of d is 0.0. Not 0.5. This is because both literals are int type.
sample
int d = 5/10;
System.out.println(d);
Since the result of is 0, it is cast to double type and becomes 0.0.
Since the calculation result is int in the calculation between int types Make either one a double literal.
sample
double d = 5.0/10;
System.out.println(d);
Or
double d = 5/10.0;
System.out.println(d);
Result is It will be 0.5.
Recommended Posts