Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]));
  }
}
like image 441
Leonne Avatar asked Dec 01 '22 02:12

Leonne


2 Answers

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).

like image 78
xlecoustillier Avatar answered Dec 16 '22 16:12

xlecoustillier


Numbers starting with 0 are octal in Java.

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

like image 40
Eel Lee Avatar answered Dec 16 '22 17:12

Eel Lee