File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Underscores in Numeric Literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Underscores in Numeric Literals" Watch "Underscores in Numeric Literals" New topic
Author

Underscores in Numeric Literals

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583


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

Tell the difficulties that i am difficult.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3097
    
  33

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".


Mohamed Sanaulla | My Blog
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

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:


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

Why using underscores?


SCJP, SCJD, SCWCD, SCBCD, SCEA
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583


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

Joined: Aug 16, 2005
Posts: 14432
    
  23

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

please help me
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Mar 10, 2008
Posts: 1836
    
    7

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

Joined: Aug 16, 2005
Posts: 14432
    
  23

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

Joined: Mar 10, 2008
Posts: 1836
    
    7

Well, 42 is the "Foo" of numeric literals
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Joined: Jul 29, 2008
Posts: 275

saloni jhanwar wrote:0_52 underscore is also at beginning.

I thought 0_52 compiled succesfully.


Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Thank you Jesper de Jong.I found something in JLS.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Joined: Mar 10, 2008
Posts: 1836
    
    7

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

Joined: Mar 10, 2008
Posts: 1836
    
    7

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

Joined: Feb 09, 2012
Posts: 583

Thanks Jelle Klap
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Aug 16, 2005
Posts: 14432
    
  23

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Mar 10, 2008
Posts: 1836
    
    7

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
Fine, if you think that's clear there, great. Do the rest of us have permission to discuss it nonetheless?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

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

Joined: Mar 12, 2011
Posts: 808
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

Joined: Mar 10, 2008
Posts: 1836
    
    7

I absolutely agree. Even as a spec it's not what I'd call perfect.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Thanks guys you all were very helpful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Underscores in Numeric Literals