go to previous page   go to home page   hear noise   go to next page


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.

Copy-and-Paste Program

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 some Java code that illustrates these points:

class IntegerDivision
  public static void main ( String[] args )
    System.out.println("The result is: " + (1/2 + 1/2) );

Copy this program to a file IntegerDivision.java, compile, and run it. See a previous chapter for details on doing this. Make some changes to the program using your text editor then run it to see the effect.

Notice the parentheses around   (1/2 + 1/2). These are necessary so that the arithmetic is done first, then the result is converted to characters and appended to the string.


What is the value of the expression 99/100 ?