MEERA :
I have two statements in java:
System.out.println(2 * (5 / 2 + 5 / 2 ));
This generate the output 8
but again in next line:
System.out.println(2 * 5 / 2 + 2 * 5 / 2 );
This generate the output 10
.
now my confusion is why it generates different output
please someone describe it. thanks
Andronicus :
Note, that division between int
s generates an int
(rounded down).
2 * (5 / 2 + 5 / 2) => 2 * (2 + 2) => 8
2 * 5 / 2 + 2 * 5 / 2 => 10 / 2 + 10 / 2 => 10
To get the exact value, you would have to use float
s:
2 * (5.0 / 2 + 5.0 / 2) == 10
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=379915&siteId=1