Java has the following syntax for different bases:
int x1 = 0b0101; //binary
int x2 = 06; //octal
int x3 = 0xff; //hexadecimal
Is there any reasoning on why it is 0
instead of something like 0o
like what they do for binary and hexadecimal? Is there any documentation on why they went this route?
1 Answer 1
Java syntax was designed to be close to that of C, see eg page 20 at How The JVM Spec Came To Be keynote from the JVM Languages Summit 2008 by James Gosling (20_Gosling_keynote.pdf):
- C syntax to make developers comfortable
In turn, this is the way how octal constants are defined in C language:
If an integer constant begins with
0x
or0X
, it is hexadecimal. If it begins with the digit0
, it is octal. Otherwise, it is assumed to be decimal...
Given above, it is natural that Java language designers decided to use same syntax as in C.
As pointed in this comment, StackOverflow's got a reasonable answer here:
All modern languages import this convention from C, which imported it from B, which imported it from BCPL.
Except BCPL used
#1234
for octal and#x1234
for hexadecimal. B has departed from this convention because # was an unary operator in B (integer to floating point conversion), so #1234 could not be used, and # as a base indicator was replaced with 0.The designers of B tried to make the syntax very compact. I guess this is the reason they did not use a two-character prefix.
-
1You can even go back further. Since C was derived from B, you will find an explanation in Thompson's B Manual section 4.1 Primary Expressions: Quote: "An octal constant is the same as a decimal constant except that it begins with a zero. It is then interpreted in base 8. Note that 09 (base 8) is legal and equal to 011. A character constant is represented by ' followed by one or two characters (possibly escaped) followed by another '. It has an rvalue equal to the value of the characters packed and right adjusted. "Jérôme– Jérôme2013年12月18日 22:19:00 +00:00Commented Dec 18, 2013 at 22:19
-
@Jérôme yup I didn't go deeper intentionally after getting original problem answered. As for the history of how C got that octal syntax, that would be a different question , that probably would better be asked and answered separately (and BTW it already was).gnat– gnat2013年12月18日 22:45:15 +00:00Commented Dec 18, 2013 at 22:45
-
@Jérôme "Note that 09 (base 8) is legal", that make no sense to me. Why would
09
be legal if the0
is meant to represent octal constants.Danny– Danny2013年12月20日 13:36:59 +00:00Commented Dec 20, 2013 at 13:36 -
@Danny offering pragmatic convenience and small syntactic perks for programmers like legalizing
09
is probably what made C language win over pure, strict, logical and oh-so-friggin' scientific crap like Pascal and other Modulasgnat– gnat2013年12月20日 13:41:26 +00:00Commented Dec 20, 2013 at 13:41 -
No, C did not import octal from B. C was developed on the PDP-11, and the natural form of binary values on that machine was octal. Assembly language was full of it. Octal shows up again in Unix permissions. Nobody even considered hex in the DEC world at that time.david.pfx– david.pfx2014年02月19日 09:53:43 +00:00Commented Feb 19, 2014 at 9:53
0o12345670
) as one of the backwards-incompatible changes of 3.0.