-3

I created an array that has 120, 200, and 016 and when i print the array, the 016 appears as 14. Why is this?

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] x = {120, 200, 016};
    for (int i = 0; i < x.length; i++) {
        System.out.print (x[i] + " ");
    }
}

Gautham M
  • 4,816
  • 3
  • 15
  • 37

1 Answers1

1

Java treats an integer with a leading 0 as a number with radix 8 (instead of using radix 10 as is the case for decimal numbers). The following demo will help you understand this concept in a clear way:

class Main {
    public static void main(String[] args) {
        int x = Integer.parseInt("16", 8);
        System.out.println(x);
        int y = 016;
        System.out.println(y);
        System.out.println(x == y);
        int z = 16;
        System.out.println(z);
        System.out.println(y == z);
    }
}

Output:

14
14
true
16
false
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110