Wierd Syntax - emphasizes between numbers

In my usual, I worked on a lot of Java UIL practice sheets when I came across this problem:

int _ = 8; System.out.println(5_5); 

Question: "What is the result of the following code fragment?"

My first guess was a syntax error, but the correct answer is actually 55 .

Why is this?

+6
source share
1 answer

From Java 7, you can have underscores between numbers to improve readability:

From JLS - Section 3.10.1 and JLS Section 3.10.2 :

Underscores are permitted as delimiters between numbers that represent an integer.

For a floating point literal also:

Underscores are permitted as delimiters between digits that indicate part of an integer, and between digits that indicate part of a fraction, and between digits that indicate exponent.

For example, 1000000000 can now be written as 1_000_000_000 . So, better for the eyes, isn't it.

Similarly, you can write - 0x7fff_ffff , 0b0111_1111 .

And regarding the variable name, _ is a valid variable name. According to the Java standard, the variable name must begin with $ , _ or letter .

+10
source

Source: https://habr.com/ru/post/951504/


All Articles