6

If I placed 00 or 0 before digits values in array output become different.

 int arr[][]=new int[3][2];
    arr[0][0]=00;
    arr[0][1]=01;
    arr[1][0]=10;
    arr[1][1]=0011;
    arr[2][0]=0020;
    arr[2][1]=21;
    for(int a[]: arr){
        for(int c : a){
            System.out.println(c);
        }

    }

Output is : 0 1 10 9 16 21

Syeda Shah
  • 81
  • 1
  • 5

3 Answers3

15

A number with a leading zero is treated as Octal.

Your 0011 is octal 8 + 1 = 9, 0020 is 2 * 8 = 16.

Note that your 00 and 01 are also being interpreted in Octal but they just happen to be the same value as their decimal counterparts.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
6

Because you are inserting numbers in base-8 (octal) format (use of 0 at the beginning) and printing them using base-10 (decimal) using Integer.toString(i, 10);

println() converts all integers to base-10 and prints them.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
5

There is a literal prefix 0 which means octal number

int decimal = 100; // 100 represented in decimal base int octal = 0144; // decimal 100 represented in octal base int hex = 0x64; // decimal 100 represented in hexadecimal base int bin = 0b1100100; // decimal 100 represented in binary base