Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 0010 give different result in array in java

Tags:

java

arrays

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

like image 996
Syeda Shah Avatar asked Apr 17 '15 13:04

Syeda Shah


People also ask

What is the value of 010 in Java?

so number starting with 0 is octal in java???... because java accepts it as an octal value, So 010 = 8.

What is the value of 010 integer literal?

I understand bitwise OR operator but why "010" equals 8? It's definitely not compliment 2's notification, so how to decode this number? That is an octal literal ! More specifically : 1*8^1 + 0*8^0 = 8 !

What is the value of integer literal 012?

Explanation: The value '\012' means the character with value 12 in octal, which is decimal 10. Note: It is equivalent to char a = 012 and int a = '\012' and int a = 012.


1 Answers

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.

like image 129
OldCurmudgeon Avatar answered Sep 23 '22 21:09

OldCurmudgeon