4

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.

Community
  • 1
  • 1
Twinone
  • 2,949
  • 4
  • 28
  • 39

2 Answers2

17

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.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
Patrick87
  • 27,682
  • 3
  • 38
  • 73
4

using leading zeroes for constants is indicating the octal notation, by which only digits 0..7 are allowed. so if you use leading zeroes for making number pretty print, that is a bad idea as the value it represents is different.

See http://docs.oracle.com/javase/specs/jls/se5.0/html/lexical.html#3.10.1 and watch out for "OctalNumeral".