Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int i=99 is working however int i = 099 not working

In Java, I noticed that when I write

int i = 99;

it works fine. However when I say

int i = 099;

I get an exception:

java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>

In my IDE, I see a red dot saying integer number too large: 099.

Why this is not getting compiled? Isn't 099 is equivalent to 99?

like image 289
Fahim Parkar Avatar asked Nov 30 '22 05:11

Fahim Parkar


2 Answers

Any leading 0s will make Java interprets the number as octal number. So, 010 is actually 8.

System.out.println(010);

OUTPUT:

8

And as you know, 8 and 9 are not allowed in an octal number.

like image 198
Eng.Fouad Avatar answered Dec 02 '22 20:12

Eng.Fouad


That's an octal number. Octal numbers are prefixed with 0 to set them apart from other values such as decimal and hexadecimal.

like image 38
Makoto Avatar answered Dec 02 '22 19:12

Makoto