Java – wierd syntax – underline between numbers

See English answers > java 7 underscore in numeric literals8

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

The question is "what is the output of the following code snippet?"

My first guess was a syntax error, but the correct response was actually 55

Why?

Solution

Starting with Java 7, you can underline numbers to improve readability:

From JLS – section 3.10 1 and JLS section 3.10 2:

For floating point literals:

For example, you can now write 1000000000 as 1_ 000_ 000_ 000. So it's better for the eyes, isn't it

Again, you can write – 0x7FFF_ ffff,0b0111_ one thousand one hundred and eleven

About variable names_ Is a valid variable name According to the Java standard, variable names should start with $_ Or start with a letter

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>