+ 1

Why does this problem print 12.0?

int x, y; double z; x = 5; y = x * 2; z = x / 2 + y; System.out.print(z);

27th Sep 2019, 3:31 PM
Juan Debenedetti
Juan Debenedetti - avatar
4 Réponses
+ 5
integer/integer will result in an integer in Java, decimal part will be removed(no rounding off).
27th Sep 2019, 6:30 PM
Gaurav Agrawal
+ 5
x=5; y=x*2;//10 z=x/2+y//2+10 Because x is a int variable then its return int value z=12.0 Because z is a double variable and its store double value using type casting
27th Sep 2019, 3:49 PM
Prathvi
Prathvi - avatar
0
Because 12 is double although not containing decimals but show decimal part
27th Sep 2019, 3:37 PM
Mohammad Zarchi
Mohammad Zarchi - avatar
27th Sep 2019, 3:54 PM
Mohammad Zarchi
Mohammad Zarchi - avatar