14

I have read that in Java7, we can now write this funny statement :

public static boolean isZero(int O_O){
  return O_O == 0_0;
}

The question is : What exactly does 0_0 mean in this context ?

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148

3 Answers3

42

Underscore characters in numerical literals are allowed in Java 7 just for the readibility purpose. From the javadocs:

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
15

In Java 7 you can add underscores to increase the readability of number literals:

int oldMillion = 1000000;
int newMillion = 1_000_000;

It's especially useful with binary data:

byte oldMax = 0b01111111;
byte newMax = 0b0111_1111;
jlordo
  • 37,490
  • 6
  • 58
  • 83
8

Underscores are valid in numbers as long as they aren't the first character, last character, or directly on either side of 0x, 0b1, etc. Basically between digits.

For example, 4_294_967_296 is a more standard use of this.

Your code will check if the int passed is equal to zero.

However, this is not a decimal int, but rather, an octal int. 0_12 does not equal 12 or 1_2. Instead, the former is equal to 10 in decimal.

1 Binary literals were added in Java 1.7.

nanofarad
  • 40,330
  • 4
  • 86
  • 117