7

Per my usual, I have been working on more UIL Java practice sheets when I came across this problem:

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

The question was "What is the output of the following code piece?"

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

Why is this?

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Will Sherwood
  • 1,484
  • 3
  • 14
  • 27

1 Answers1

11

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

From JLS - Section 3.10.1 and JLS Section 3.10.2:

Underscores are allowed as separators between digits that denote the integer.

For floating point literal also:

Underscores are allowed as separators between digits that denote the whole-number part, and between digits that denote the fraction part, and between digits that denote the exponent.

For e.g., 1000000000 can now be written as 1_000_000_000. So, it is better for eyes, isn't it.

Similarly you can write - 0x7fff_ffff, 0b0111_1111.

And regarding variable name, _ is a valid variable name. As per Java standard, a variable name should start with a $, _ or a letter.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 3
    +1 didn't know you couldn't use this for floating point. – Peter Lawrey Aug 11 '13 at 19:27
  • I think this is heinous. Improve readability? I disagree. – duffymo Aug 11 '13 at 19:27
  • 2
    @duffymo. I think this was for readability purpose majorly. Writing `1_000_000_000` is better readable than `1000000000`. – Rohit Jain Aug 11 '13 at 19:28
  • 1
    It make sense for long numbers like `1_000_000_000` I am used to writing `1000*1000*1000` – Peter Lawrey Aug 11 '13 at 19:28
  • I'm more hung up on the fact _ is a valid variable name (never knew that). Seems horrible! – Andrew Martin Aug 11 '13 at 19:28
  • 1
    @RohitJain "better readable" it is. – Peter Lawrey Aug 11 '13 at 19:29
  • 1
    @AndrewMartin That is just the start there is loads of symbols you can use, some of them have a zero width or make every character after them appear backwards, mwahahaha. http://vanillajava.blogspot.co.uk/2012/09/hidden-code.html and http://vanillajava.blogspot.co.uk/2012/08/uses-for-special-characters-in-java-code.html – Peter Lawrey Aug 11 '13 at 19:31
  • @PeterLawrey: If I ever get made redundant, I know what I'm doing on my last day :) – Andrew Martin Aug 11 '13 at 19:32
  • @PeterLawrey. Those are some nice variable names. I should start to use it, when I want to scare fellow programmers :) – Rohit Jain Aug 11 '13 at 19:33
  • ⁀ ‿ ⁀ Try running this code in a main method, This really does compile and run. It print a list of the odd characters you can use `for (char c‮h = 0; c‮h < Character.MAX_VALUE; c‮h++) if (Character.isJavaIdentifierPart(c‮h) && !Character.isJavaIdentifierStart(c‮h)) System.out.printf("%04x <%s>%n", (int) c‮h, "" + c‮h); ` ..................................................... So backward lol – Peter Lawrey Aug 11 '13 at 19:37
  • @PeterLawrey. Auuwaa. Auwaaa. That's like a suicide pill. – Rohit Jain Aug 11 '13 at 19:40
  • @PeterLawrey. ROFL.. I can't go forward, 'coz I've to go backward to move forward. Holy freak, it does give an output. That is evil. ;) – Rohit Jain Aug 11 '13 at 19:41
  • 2
    @PeterLawrey. Seems like underscores work for floating point literals too - `12.145_34` compiles perfectly fine. Though it's not specified in JLS. :( I guess, I'll give a deeper look. – Rohit Jain Aug 11 '13 at 19:42
  • @RohitJain I bet the parser for integers and floating point share code because it didn't matter before. – Peter Lawrey Aug 11 '13 at 19:48
  • @PeterLawrey. Well, it is actually defined for numerical literal. So, floating point literal also comes in. I missed that part earlier. – Rohit Jain Aug 11 '13 at 19:49