0

Possible Duplicate:
Integer with leading zeroes

Hi.

How does Java deal with bytes, ints, shorts and longs prefixed by zeroes, e.g.

// Prints 8.
System.out.println(010);

// Prints 64.
System.out.println(0100);

So, 8^(n-1), I guess. But why?

Explanations are appreciated!

EDIT: So that's how it works. However, nobody knows why, it seems, and the other topic is dead.. Gn people. :)

Community
  • 1
  • 1
whirlwin
  • 16,044
  • 17
  • 67
  • 98
  • A lot of us want to know *why* Java borrowed this from C. I don't think we're ever going to get a satisfactory answer. :-) – Ken Dec 11 '10 at 02:01
  • 1
    See [Octal number literals: when? why? ever?](http://stackoverflow.com/questions/44569/octal-number-literals-when-why-ever) for discussion about the *why* behind this question. – Greg Hewgill Dec 11 '10 at 02:34

2 Answers2

3

A zero prefix is interpreted as an octal number.

10 in octal is 8 in decimal; 100 in octal is 64 in decimal.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
0

Search for 'octal' on this page: http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

mjomble
  • 521
  • 3
  • 9
  • 20
  • *The prefix 0 indicates octal, whereas 0x indicates hexadecimal.* Hex values are often prefixed with 0x; I see that all the time, but why are octal numbers prefixed with 0? – whirlwin Dec 11 '10 at 02:04