• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

doubts about underscore usage in numeric values

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm starting to learn java and I can't understand why a numeric variable can have a single underscore, but when they are two, java it does not compile anymore.
For example, this is correct:


but this is not:


 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because the underscore is a substitute character for a comma. It's easier to read long numbers when they're broken up. 123456789 is less readable to humans than 123,456,789. I think it was in Java 7, they allowed underscores in numbers to make larger numbers like this more readable. "1_234_" is therefore the same as writing "1,234," which is not a valid value. The compiler is trying to be helpful and indicate this. Just think of it this way: if you replace the underscores with commas, is it a valid number that makes sense? Otherwise you'll get the illegal underscore message from the compiler.

So minor nitpick: you can have more than one underscore, if your number still makes sense: e.g. 1_234_567 would be valid.
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the Oracle/Java technote on this: https://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html - hope that helps
 
Marshal
Posts: 8963
646
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

Answer is, because placing an underscore has some rules.
1. It can't be before very first digit
2. It can't be after the last digit
3. It can't be prior or next to radix point
 
Marshal
Posts: 79964
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott Shipp wrote:. . . I think it was in Java 7 . . .

Yes, it was. The link you found explains it all and there is a section in the Java™ Tutorials which appears to say exactly the same
Not being in USA, I don't have a social security number, but it appears that the format of those numbers may be threeDigits‑twoDigits‑fourDigits; If you have a “number” whose format is specified like that, I think it ceases to be a number and becomes a String containing digits and no letters. The same applies to house numbers. If it's a number, you should be able to do arithmetic with it and vice versa. You never do arithmetic with house numbers.
 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mauricio,
You have already gotten answers for your question but i would like to add a bit more info.
If you'll be use Binary, Octal or Hexadecimal numbers you should be aware also some points.

You might know binary system use 0 and 1(base 2). Same rules are valid as Liutauras Vilda's mentioned.
You can't use underscore beginig or ending ex;
int i = 0b_111; // won't compile
int i1 = 0b1_11 // compilse

Octal number uses between 0 to 7 (base 8)
int i2 = 0_123; // won't compiles
int i3 = _0123;  // compiles

Hexidecimal num uses 0 to 9 and letter A or a to F or f ( A is 11 B is 12 ... 15 is F)
int i4 = 0x_9A; // won't compile
int i5 = 0x9_A; // compiles

By the way do not use underscore for object arguments
ex; Integer i = new Integer("123_456"); // NumberFormatException
 
Mauri Mura
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. Each one of all answers has been very useful.
 
Emrik SjöKa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emrik SjöKa wrote:Hi Mauricio,
You have already gotten answers for your question but i would like to add a bit more info.
If you'll be use Binary, Octal or Hexadecimal numbers you should be aware also some points.

You might know binary system use 0 and 1(base 2). Same rules are valid as Liutauras Vilda's mentioned.
You can't use underscore beginig or ending ex;
int i = 0b_111; // won't compile
int i1 = 0b1_11 // compile

Octal number uses between 0 to 7 (base 8)
[strike]int i2 = 0_123; //  compiles
int i3 = _0123;  // won't  compile

Hexidecimal num uses 0 to 9 and letter A or a to F or f ( A is 11 B is 12 ... 15 is F)
int i4 = 0x_9A; // won't compile
int i5 = 0x9_A; // compiles

By the way do not use underscore for object arguments
ex; Integer i = new Integer("123_456"); // NumberFormatException

 
Campbell Ritchie
Marshal
Posts: 79964
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emrik SjöKa wrote:. . .
int i5 = 0x9_A; // compiles

. . . but isn't a number literal. Have a look at the two links posted yesterday.

. . .  do not use underscore for object arguments
ex; Integer i = new Integer("123_456"); // NumberFormatException

That is because nobody writes numbers 123_456 in real life. Only in programming.

If I remember correctly, that format was permitted in the Eiffel language 30 years ago.
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic