Why 0010 gives a different result in an array in java

If I put 00 or 0 in front of the digits, the values ​​in the array will be 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: 0 1 10 9 16 21

+6
source share
3 answers

A number with a leading zero is treated as Octal .

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

Note that your 00 and 01 also interpreted in Octal, but they just match their decimal counterparts.

+15
source

Since you insert the numbers in base-8 (octal) format (use 0 at the beginning) and print them using base-10 (decimal) with Integer.toString(i, 10);

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

+6
source

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

+5
source

Source: https://habr.com/ru/post/985351/


All Articles