Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a long start with zero? [duplicate]

Possible Duplicate:
09 is not recognized where as 9 is recognized

Just wondering, when I declare the following:

public static final long __VERSIONCODE = 0L;
public static final long __VERSIONCODE = 9L;

that will work, but whenever I try this:

public static final long __VERSIONCODE = 09L;
public static final long __VERSIONCODE = 08235L;

I get an error (in eclipse):

"The literal 09L of type long is out of range."

So I thought that was because it started with a zero.

but then I tried with the second digit lower as eight:

public static final long __VERSIONCODE = 07235L;

which gives me NO error.

then:

public static final long __VERSIONCODE = 07239L;

gives me also an error too.

So I really don't get the point of which values I can assign to a long and which I can't. Why do I get this errors? (It's actually just that I'm curious, I can also just use a String for my version code).

Also, I forgot to mention that this behaves exactly the same using doubles instead of longs.

like image 910
Twinone Avatar asked Nov 29 '22 13:11

Twinone


1 Answers

When you put a 0 in front of an integer-type literal, it will interpret it as representing an octal number. Since "9" isn't a valid digit for octal numbers, that might be what's going on. Try printing out the (decimal) value of "010L" and see whether is says "8" to confirm.

Note: not sure if Java does this, or if this is purely an artifact of Eclipse. If the latter, printing out 010L would show 10. If the former, you'd see 8. If it's just an artifact of Eclipse, you can confirm by trying 01L, 02L, ..., 07L, which should all work, and 08L and 09L which would fail.

like image 152
Patrick87 Avatar answered Dec 19 '22 01:12

Patrick87