• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Underscores in Numeric Literals

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 193
Mac OS X Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why using underscores?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please help me:argh:
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:please help me:argh:


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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:please help me:argh:



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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Deems wrote:

saloni jhanwar wrote:please help me:argh:


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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, 42 is the "Foo" of numeric literals
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:

Dennis Deems wrote:

saloni jhanwar wrote:please help me:argh:


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.
 
Ranch Hand
Posts: 275
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jelle Klap
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 5116
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 5116
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys you all were very helpful.
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic