So, which of the following attempts at leveraging the new Java 7 feature of using numeric underscores with literals actually uses the syntax properly?
int overflow = 0b1010_1010_1010_1010_1010_1010_1010_1011;
long bow = 0b1__01010101__01010101__01010101__01010111L;
int x_ = 1_;
int _1 = 12345;
int x = _1;
double pious2 = 3._14;
float myBoat1 = 321.123_F;
float myBoat2 = 321.1_23F;
int hexed = 0_x_BABE;
Rules for using binary notation
Well, it's easier figuring out what works and what doesn't if you stick to the following four rules:
- Underscores can't go at the beginning or the end of a number.
- You can't use an underscore on either side of a decimal.
- The underscore cannot go before an identifying suffix such as F, D or L
- You can't put an underscore before or after the binary or hexadecimal identifiers b and x.
Expanding on Java 7
We've got a simply little tutorial on the topic if you want to brush up on the ins and outs of this new Java 7 feature, which expands on some of the other concepts that were discussed in the article on another new Java 7 feature, binary notation.
Related Books and Resources
What's New in Java 7? by Madhusudhan Konda
Java 7: A Beginner's Tutorial by Budi Kurniawan
OCP Java SE 7 Programmer Study Guide by Kathy Sierra
Java 7 and Polyglot Programming on the JVM by Benjamin J Evans