Is there any particular reason why this kind of literal is not included whereas hex and octal formats are allowed?
-
It is not included in most languages, maybe the same reason for it not being included in Java. But I would like to know the general reason anyway. – Shamim Hafiz - MSFT Oct 25 '10 at 09:55
-
Thanks guys for the quick responses. In summary, I would say it is not included simply because C doesn't have it and not really because it adds complexity. Heck, octal is included and I don't see much use to it compared to binary but only because C has it. As we can see from this proposal http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000929.html , binary literals has lots of advantages especially in the bit oriented domain. – Adrian M Oct 25 '10 at 10:36
6 Answers

- 13,577
- 18
- 69
- 108
-
8Here's a more recent and more "official" reference - http://openjdk.java.net/projects/coin/ – Stephen C Oct 25 '10 at 10:06
-
1Great link +1. It lead me to this http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000929.html which sums up all my reasons why I would want to see a binary literal. – Adrian M Oct 25 '10 at 10:25
-
2There's the more recent link: http://download.oracle.com/javase/7/docs/technotes/guides/language/binary-literals.html – Thomas Rawyler Nov 11 '11 at 12:57
Binary literals were introduced in Java 7. See "Improved Integer Literals":
int i = 0b1001001;
The reason for not including them from day one is most likely the following: Java is a high-level language and has been quite restrictive when it comes to language constructs that are less important and low level. Java developers have had a general policy of "if in doubt, keep it out".
If you're on Java 6 or older, your best option is to do
int yourInteger = Integer.parseInt("100100101", 2);

- 413,195
- 112
- 811
- 826
-
4C is mid-level language and has historically been quite permissive when it comes to language constructs that are very low level. Still, no binary literals. – Fred Foo Oct 25 '10 at 13:04
-
1Yes, that's IMO stranger. It has been present in GCC for quite a while though as I understand it. – aioobe Oct 25 '10 at 13:06
-
1How many people find octal literals more useful than they would find binary ones? – supercat Jan 14 '14 at 20:48
actually, it is. in java7.
http://code.joejag.com/2009/new-language-features-in-java-7/

- 41
- 1
- 7
The associated bug is open since April 2004, has low priority and is considered as a request for enhancement by Sun/Oracle.
I guess they think binary literals would make the language more complex and doesn't provide obvious benefits...

- 258,201
- 41
- 486
- 479
Java 7 does allow binary literals ! Check this: int binVal = 0b11010; at this link: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

- 29
- 8
There seems to be an impression here that implementing binary literals is complex. It isn't. It would take about five minutes. Plus the test cases of course.

- 305,947
- 44
- 307
- 483