0

I am trying following program.

int var = 012;
int result = var % 10;

output:

result = 0

I am not able to understand why java is not able to consider 012 as 12.

Sam Hanley
  • 4,633
  • 7
  • 35
  • 58

1 Answers1

12

In Java, integer literals that start with 0 are interpreted as octal numbers.

So, 012 is the number 1 x 8 + 2 = 10 (in decimal), not 12 (decimal).

012 % 10 == 10 % 10 == 0

Jesper
  • 195,030
  • 44
  • 313
  • 345