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

Answer:

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 write 1.0/2.0 + 1.0/2.0 because now the decimal points make each number a double. If just one of the operands of an operator is double, then the other operand is automatically promoted to a double. Here is some Java code that illustrates these points:

public 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. (Or use one of the web-page Java compilers mentioned in the Table of Contents.) 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.

Edit and run the program to see each of these variations:



Expression ? Result
(1/2 + 1/2)
(1.0/2 + 1/2)
(1/2.0 + 1/2)
(1/2 + 1.0/2.0)
(1/2.0 + 1.0/2.0)

QUESTION 8:

What is the value of the expression 99/100 ?


go to previous page   go to home page   go to next page