New Java 7 Features: A Numeric Underscores with Literals Tutorial

If you're doing the OCPJP upgrade exam, or just hitting the full blown OCPJP 7 Java certification, you need to know the ins and outs of the new language features. One fun and friendly syntax introduced with Java 7 is using numeric underscores. It's also an objective on the OCPJP7 exam: Using Numeric Underscores with Literals

In the previous tutorial that dealt with binary notation, things got a little ornery when we started expressing 32 bit int and 64 bit long data types using binary notation. Aw, who are we kidding – it was ornery just expressing 8-bit and 16-bit byte and short data types. Seriously though, looking at the initializations below, could you quickly conclude how many ones and zeros were in each of those binary numbers?

int overflow = 0b10101010101010101010101010101011;
long bow = 0b101010101010101010101010101010111L;

Now, what if I asked the question again, using the following code:

int overflow = 0b1010_1010_1010_1010_1010_1010_1010_1011;
long bow = 0b1__01010101__01010101__01010101__01010111L;


Rules for using Numeric Literals with Underscores in Java 7

With the first binary number being broken into quartets by separating every fourth number by an underscore, or with the second binary number being broken up into octets, separate by two underscores, the numbers become much more readable. Declaring numeric literals with underscores interspersed within the digits would have causes a compile error in any previous release of the language, but in Java 7, numeric literals with underscore characters are not only legal, but they're highly encouraged.

Now, there are a four key rules you need to pay attention to when using numeric literals with underscore characters.

  1. Underscores can't go at the beginning or the end of a number.
  2. You can't use an underscore on either side of a decimal. 
  3. The underscore cannot go before an identifying suffix such as F, D or L
  4. You can't put an underscore before or after the binary or hexadecimal identifiers b and x.

Let's take a look at these rules in more detail.

Underscores can't go at the beginning or the end of a number

This attempt at using an underscore at the termination of a number:

int x = 1_;

results in the following compile error:

Underscores have to be located within digits

Interestingly, flipping the underscore to the preceding side of the digit generates a different error message. Take a look at the following line of code:

int x = _1;

This code generates a compiler error that says: _1 cannot be resolved to a variable

You see, when you use a leading underscore, the JVM thinks you're referring to a variable, so it gives you the error that the variable name cannot be resolved. It's as though the compiler is expecting some code that looks like this:

int _1 = 12345;
int x = _1;

The bottom line? Don't start or finish a numeric literal with an underscore.

You can't use an underscore on either side of a decimal

So, the following three lines of code would all kill the compiler:

double pious1 = 3_.14;   //fail
double pious2 = 3._14;   //fail
double pious3 = 3_._14;  //fail

The underscore cannot go before an identifying suffix such as F, D or L 

So, all three of the following attempted initializations will fail:

double trouble = 123.456_D;  //fail
float myBoat = 321.123_F;  //fail
long winded = 90210_L;  //fail

However, with a slight adjustment of the location of the underscore, each of these could be made to work, as follows:

double trouble = 1_23.456D;  //valid
float myBoat = 321.1_23F;  //valid
long winded = 902_10L;  //valid

You can't put an underscore before or after the binary or hexadecimal identifiers b and x

So the following attempted initializations are not valid:

byte size = 0_b111101;  //fail
byte me = 0b_111101;  //fail
int hexed = 0_x_BABE;  //fail

But other than that, you're in the clear to intersperse underscores wherever and as often as you see fit. Numeric underscores are a great enhancement to the language, and knowing when and how to use them is going to help you nail a certification question or two if you ever work up the gumption to tackle the Oracle Certified Professional, Java 7 SE Programmer (OCPJP) designation.

 

Learning Resources for the Java and Java 7 Certification

 

OCP Java SE 6 Programmer Practice Exams (Exam 310-065) (Certification Press)
OCP Java SE 7 Programmer Study Guide (Certification Press)
SCJP Sun Certified Programmer for Java 6 Exam 310-065
A Programmer's Guide to Java SCJP Certification: A Comprehensive Primer (3rd Edition)
SCJA Sun Certified Java Associate Study Guide for Test CX-310-019, 2nd Edition

 

Check out these other tutorials from TheServerSide:

New Java 7 Features: Binary Notation and Literal Variable Initialization
New Java 7 Features: Numeric Underscores with Literals Tutorial
New Java 7 Features: Using String in the Switch Statement Tutorial
New Java 7 Features: The Try-with-resources Language Enhancement Tutorial 
New Java 7 Features: Automatic Resource Management (ARM) and the AutoCloseable Interfact Tutorial
New Java 7 Features: Suppressed Exceptions and Try-with-resources Tutorial
Java 7 Mock Certification Exam: A Tricky OCPJP Question about ARM and Try-with-Resources
OCAJP Exam to Debuts in March 2010. OCPJP Released in June?
OCPJP & OCAJP Java 7 Exams: Oracle Drops the Training Requirement

OCAJP and OCPJP Changes for Java 7:  New Objectives, a Format Change and a Price Hike

Next Steps

New to Git and distributed version control? Here are some Git examples and Jenkins-Git integration tutorials designed to help you master the popular source code versioning tool.

Dig Deeper on Front-end, back-end and middle-tier frameworks

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close