Strange array behavior in Java

This is a simple array declaration and initialization

int arr[] = new int[10];

    for(int i = 0; i<arr.length; i++){

    arr[i] = i;
    }

this

System.out.println(arr[000001]);

to

System.out.println(arr[000007]);

Print out the correct value, but any value higher than 8

System.out.println(arr[000008]);

Generate Java Lang.runtimeexception: unable to compile source code for

Why is that?

Solution

This is array independent; Integers starting with the number 0 are octal (base 8) The legal octal digit is 0-7, so 08 (or 00000008) is an invalid octal integer literal The correct octal of 8 is 010

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>