- 1

why is "result" equals 0.0 in eclipse? (code in the message)

class MyClass{ public static void main(String[]args) { int x=1; int y=3; double result=x/y; System.out.println(result); } }

10th Dec 2016, 10:31 PM
Henrique Machado
Henrique Machado - avatar
1 Odpowiedź
+ 2
Because even if the var where its saved is doubled, you are dividing int. You can solve that Making the 1 and 3 doubles, or using Promotion: double result= (double) x/y;
10th Dec 2016, 10:40 PM
Nahuel
Nahuel - avatar