Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of 0_0 in Java 7 [duplicate]

Tags:

java

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 ?

like image 805
Arnaud Denoyelle Avatar asked Jul 29 '13 16:07

Arnaud Denoyelle


People also ask

What does D mean in double Java?

D stands for double. F for float. you can read up on the basic primitive types of java here.

Why and how underscores are defined in literals?

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.


3 Answers

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

like image 72
Juned Ahsan Avatar answered Oct 21 '22 02:10

Juned Ahsan


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;
like image 15
jlordo Avatar answered Oct 21 '22 01:10

jlordo


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.

like image 8
nanofarad Avatar answered Oct 21 '22 01:10

nanofarad