简体   繁体   中英

why is this array to 2d array boolean true?

Hi I happen to come across a code that my friend sent me, and am having trouble making out the second part of the print statement.. a[3] is 4, which is in row 0 column 2 ( b[0][2] ), but why is a[2] which is 53, proving true to b[2][1] == 43 ??? (The code prints 'true true' btw.)

class Ex1{
     public static void main(String[] args) {

         int a[] = { 1,2,053,4};
         int b[][] = { {1,2,4} , {2,2,1},{0,43,2}};

         System.out.print(a[3]==b[0][2] );
         System.out.print(" " + (a[2]==b[2][1]));
  }
}

This happens because 053 is an octal number equal to 43 in decimal.

The 0 prefix denotes an octal value in Java and some other languages (Perl, Ruby, C and derived, Javascript to name a few).

Numbers starting with 0 are octal in Java.

And in your case 43 decimal is equal to 053 octal.

In Java and several other languages, an integer literal beginning with 0 is interpreted as an octal ( base 8 ) quantity. Here 053 is an octal number, which is value of 43 in base 10 .

JLS says

An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.

在Java中,八进制数从0开始。因此,八进制53等于十进制43。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM