1/2 + 1/2 = 0
It looks as though this is a mistake, but no: each 1/2 calls for integer division, resulting in an integer 0. The two zeros are added to get the final answer, zero.
If you really want to add one half to one half you should write
1.0/2.0 + 1.0/2.0
because now the decimal points make each number a double.
Here is a Java application that illustrates these points:
class IntegerDivision { public static void main ( String[] args ) { System.out.println("The result is: " + (1/2 + 1/2) ); } }
You can run this program by copying it to
a file IntegerDivision.java
, compiling and running it.
See a previous chapter for details on doing this.
It would be nice if you made a few changes to the program
using your text editor then ran the program to see the effect.
Notice the parentheses around (1/2 + 1/2)
What is the value of the expression 99/100 ?