Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Underscores in Numeric Literals

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

F:\Underscores in Numeric Literals>javac _.java
_.java:6: error: illegal underscore
byte me = 0b_111101;// not valid
^
_.java:7: error: illegal underscore
int x6 = 0x_52;//then how it is valid ?
^
2 errors
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0x_ is used to represent hexadecimal values. Java 7 has introduced a syntax for representing binary values where in you can use "b" or "B".
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only one of those assignments is valid, and only as of Java 7:


The other two are not valid, because an underscore can only appear between digits in a numeric literal.
This would be valid:

 
Jiafan Zhou
Ranch Hand
Posts: 193
Fedora Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why using underscores?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I've mindset that as 0x is used to represent hex number same as 0 at 0_52 is being used to represent octal number so actual number will be 52.Hence _ before 52 should not be valid.please help
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to know the exact rules for this, then it's time to look in the Java Language Specification. Paragraph 3.10.1 specifies the notation for integer literals. Read this paragraph carefully; it explains exactly where underscores are allowed and not allowed for decimal, hexadecimal, octal and binary integer literals.

If you read it carefully you'll see that 0x_52 is not valid, and 0_52 is valid. If you want to know why it was defined in this way, then that's a question you should ask the person who defined those rules. The specification doesn't really explain why the designer chose to make 0x_52 invalid. It's just how it is.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:please help me

It might help to read this, as it explains the rationale: http://mail.openjdk.java.net/pipermail/coin-dev/2009-April/001628.html
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:please help me


There's nothing else to explain, really. Like Jesper said, this is simply how it's defined in the JLS.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:
saloni jhanwar wrote:please help me

It might help to read this, as it explains the rationale: http://mail.openjdk.java.net/pipermail/coin-dev/2009-April/001628.html

Funny. That mail also uses the number "52", exactly as Saloni in her examples. That can't be a coincidence...

And it gives the following reason why 0_52 is allowed and 0x_52 is not:
The mail wrote:Underscores must always be between digits
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, 42 is the "Foo" of numeric literals
 
Henry Wong
author
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jiafan Zhou wrote:Why using underscores?


While the main conversation is going on, let me tackle the side question.... Let's say you are working on a 10 gigE network, and need to confirim how close you are using the 10 million bits of bandwidth. So, you have a constant in your application....

public int rate = 10000000;

Is this constant really readable? Or do you wind up going back, ever so often, to confirm that it has the correct number of zeros? Now....


public int rate = 10_000_000;

Doesn't this look much much better ?


Henry
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
Dennis Deems wrote:
saloni jhanwar wrote:please help me

It might help to read this, as it explains the rationale: http://mail.openjdk.java.net/pipermail/coin-dev/2009-April/001628.html

Funny. That mail also uses the number "52", exactly as Saloni in her examples. That can't be a coincidence...

And it gives the following reason why 0_52 is allowed and 0x_52 is not:
The mail wrote:Underscores must always be between digits


Yes,underscores must always be between digits but there is also an another rule:
Underscores can't go at the beginning or the end of a number.
i.e 0x_52 is invalid because underscore was at beginning, so for octal number 0_52 underscore is also at beginning.
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:0_52 underscore is also at beginning.

I thought 0_52 compiled succesfully.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jesper de Jong.I found something in JLS.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aniruddh Joshi wrote:
saloni jhanwar wrote:0_52 underscore is also at beginning.

I thought 0_52 compiled succesfully.


Yes,it will be compile successfully because compiler is not treating it as octal number.It is being treat as decimal number.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:Thank you Jesper de Jong.I found something in JLS.


Which would presumably be this excerpt from paragraph 3.10.1. Integer Literals :


In a hexadecimal or binary literal, the integer is only denoted by the digits after the 0x or 0b characters and before any type suffix. Therefore, underscores may not appear immediately after 0x or 0b, or after the last digit in the numeral.

In a decimal or octal literal, the integer is denoted by all the digits in the literal before any type suffix. Therefore, underscores may not appear before the first digit or after the last digit in the numeral. Underscores may appear after the initial 0 in an octal numeral (since 0 is a digit that denotes part of the integer) and after the initial non-zero digit in a non-zero decimal literal.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:
Aniruddh Joshi wrote:
saloni jhanwar wrote:0_52 underscore is also at beginning.

I thought 0_52 compiled succesfully.


Yes,it will be compile successfully because compiler is not treating it as octal number.It is being treat as decimal number.


It is, in fact, interpreted as an ocal / base 8 literal value.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jelle Klap
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:There's nothing else to explain, really. Like Jesper said, this is simply how it's defined in the JLS.

Actually, it helped me rather a lot to learn that the intent of the underscores is merely to enhance human readability of long values. I don't see that anywhere in the JLS.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:Actually, it helped me rather a lot to learn that the intent of the underscores is merely to enhance human readability of long values. I don't see that anywhere in the JLS.

That's because the JLS, like most technical specifications, is primarily meant to specify in detail how Java works - not why it works that way.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, in general. But "there's nothing left to explain" seems to preclude the possibility of further discussion beyond what's in the JLS. Dennis had a helpful point to make that was not covered in the JLS, so he made it.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It actually pretty much is.
Underscores are allowed as separators between digits that denote the integer

Separators, in the context of numbers, are usually intended for the sole purpose of improving readability, no?
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fine, if you think that's clear there, great. Do the rest of us have permission to discuss it nonetheless?
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, of course! I don't think I ever implied otherwise? And if I did I sure didn't intend to.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:It actually pretty much is.
Underscores are allowed as separators between digits that denote the integer

Separators, in the context of numbers, are usually intended for the sole purpose of improving readability, no?

Cool, thanks, I totally missed that line. This is far from my first encounter with the JLS, but I frequently have difficulty drawing useful information from it. I think, in general, the JLS is one of the less helpful sources to which we can direct beginning Java programmers. They probably can get more help from a clear, human-readable text.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I absolutely agree. Even as a spec it's not what I'd call perfect.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys you all were very helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic