Search code examples
javaintegeroperatorsequality-operator

why is not (123 == 0123) in java?


I am developing an application in Android using Eclipse. I wrote the following code and in tests the first and third "if" block is not reachable. Why?

When I add a leading zero to a number, the equal operator returns false.

int var = 123;
if (var == 0123) {
    //not reachable
}
if (var == 123) {
    //reachable
}
if (var == (int)0123) {
    //not reachable
}
if (var == (int)123) {
    //reachable
}

Solution

  • 0123 is an octal number (leading 0), while 123 is a decimal number.

    so 0123 actually equals to 83.