Jesper de Jong wrote:Because the float and double data types are not infinitely precise; you can get rounding errors when you start doing calculations with them.
yamuna dulanajani wrote:
this can compile ,but
this gives a compile error ,
WHY?
Jelle Klap wrote:Yeah, that's what I figured. The difference is that in the first case x is a compile time constant. i.e. the compiler knows that x will always have value 101, and by extension it knows that the if-condition will always be true and y will always be initialized to value -1. That's why it's legal to use y in the System.out.println() call: the compiler knows it was initialized to a value. In the other code sample x isn't a compile time constant and all the implications that held up in the first code sample don't. This leads the compiler to complain about the use of y, because it can't figure out whether or not it was initialized. At runtime, this is clearly the case, but the compiler just isn't smart enough to pick up on that.