Ch2 – Rounding Errors & Operators for Strings


Type the following in DrJava’s Interactions Pane and note the discrepency in results.

> System.out.println(0.1* 10);
1.0
> System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
0.9999999999999999
  1. Type the following in DrJava’s Interactions Pane and note the output.
> System.out.println(1 + 2 + "Hello");
3Hello
  1. Now type the following and note the difference from the previous output.
> System.out.println("Hello" + 1 + 2);
Hello12
  1. Now use parenthesis to force evaluation of 1+2 prior to concatenating to the string.
> System.out.println("Hello" + (1 + 2));
Hello3