Java Syntax – Reasoning Behind Octal Notation

javalanguage-design

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?

Best Answer

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 or 0X, it is hexadecimal. If it begins with the digit 0, 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.

Related Topic