How zero affects the result in the division method

int a=0234;
int result=a/100;
System.out.println("result is " + result);

output:

the result is 1

Why resultnot 2?

+4
source share
6 answers

Because 0234- this is the number Octal.

When you add a zero in front of a numeric constant, Java treats it as an Octal number .

If you convert it to decimal, then it is equal 156, dividing it by decimal 1001 (due to integer division).

+6
source

0234represents the octal number , which is equal to which .2 * 82 + 3 * 81 + 4 * 80156

Then 156/100 = 1 (due to integer division )

+7

. , 0234 - 156, 234.

, 156/100 = 1.

:

+2

0234 - 156. 1. . JLS 3.10.1:

ASCII 0, ASCII 0 7, , , .

+2

Integer literals starting with 0 are interpreted as octal rather than decimal. So, 0234in fact, the octal value for 156 decimal places. Therefore, 156/100 = 1 in integer divisions.

+1
source

When you declare an int starts at 0 and does not contain digits exceeding 7 java, consider this an octal number. Therefore, 0236 is actually 236 base 8, which is 156. Then 156/100 = 1

+1
source

All Articles