2

I'm very new and I'm just curious to know why this code outputs 201.0 when entering 2E2 as the value of the double.

public static void main(String[] args) {
    double r = 2E2;
    try{
    }
    catch(InputMismatchException e) {
        r=-1.0;
    }
    finally{
        r++;
    }
    System.out.println(r);
}

output: 201.0

Newguy123
  • 21
  • 1

2 Answers2

3

By putting in the E you are saying 2 * 10 ^ 2 power. The additional 1 comes from the finally block which is always executed even if there is an error thrown. If you were to put 2E3 it would print out 2001.0!

Marcus Cantu
  • 463
  • 3
  • 14
0

2E2 in decimal notation is 200. Your try catch block does not contain any code, so it never reaches the exception block and the finally block increments 2E2 by adding 1 to it. So, the answer is 2E2 + 0X1 = 0x2E3 (201.0), which is the correct. Finally, when you print it, it is displayed in the decimal format as 201.0.

Ashish
  • 209
  • 1
  • 10