JDK-5025288 : Language support for literal numbers in binary and other bases
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.0,1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,windows_xp
  • CPU: generic,x86
  • Submitted: 2004-04-01
  • Updated: 2014-02-26
  • Resolved: 2011-08-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 rcFixed
Related Reports
Duplicate :  
Relates :  
Description
Name: rmT116609			Date: 04/01/2004


A DESCRIPTION OF THE REQUEST :
It's alway struck me as strange that there is no way of writing binary numbers in Java (or C++). Many languages have a syntax for binary literals.

Can I suggest a minor language change allowing for numbers in any base, using a simple syntax such as 2_10111. In practise this would mostly be used for binary, but would be occasionally useful for other bases up to 36.


JUSTIFICATION :
Argument in favour:
It is still a common programming technique to use sets of flags, indeed this is a major use of the forthcoming enum feature.

Flags in hex and decimal are difficult to read, and particularly when a number contains more than one flag. Who could read 0xD9 at a glance? The clearest current representation would be
(1<<7) + (1<<6) + (1<<4) +(1<<3) + (1<<0)
which is clumsy and error-prone, unlike (say) 2_11011001.

Another benefit: writing a set of flags as (say) 2_11011001 clearly expresses that the number is to be interpreted as flags, rather than a true integer.

Another minor benefit: Java's leading-zero octal notation is very unclear and it's easy to switch to octal by accidental as a result of a typo. It could be deprecated (maybe even producing a compiler warning!) in favour of something like 8_...

Argument against: there is resistance to adding to the language in case it increases complexity. Fair enough, but I think this addition actually reduces complexity.

CUSTOMER SUBMITTED WORKAROUND :
Use hex, octal or decimal to express other bases.
(Incident Review ID: 201283) 
======================================================================

Comments
EVALUATION This is syntactic sugar, but a nice feature for beginners and experts alike. Other bases and the proposed <base>_<literal> syntax seem overkill. A binary literal would be unsigned, so that 0b1111111 means 127 and not -1 as in two's-complement. Ideally, the largest binary literal would be 0b11111111111111111111111111111111 (32 bits).
07-12-2006