-3

int x = 100_000_5; System.out.println(x);

Instead of throwing an error it prints the result 1000005. If this is a valid case what other abnormalities are there with Integer?

  • `100_000_5` would be an unusual usage. It's for making long numbers easier to read. Just as we use commas when writing a million (1,000,000) it's helpful if we can do something similar in our code: `1_000_000`. – Michael Jan 04 '19 at 16:12
  • 1
    See the language spec's definition of [Integer literals](https://docs.oracle.com/javase/specs/jls/se9/html/jls-3.html#jls-3.10.1), which fully describes everything about them. You may be surprised by other things like a literal being intepreted in [octal if it starts with a zero](https://ideone.com/TFnDyv). – Andy Turner Jan 04 '19 at 16:14
  • 1
    @Michael I guess the reason why they didn't limit it to certain places is because integers might contain groups of digits with certain special meanings (like in phone numbers, bar codes, amounts of currency etc. - although it might be unwise to represent them as int) and typical grouping is culture-dependent (some locales place grouping separators every 2 digits if I recall correctly) – Hulk Jan 04 '19 at 16:21

1 Answers1

3

int x = 100_000_5 is same as int x = 1000005. JVM7 allows to write literals with more readable format, e.g. separate groups.

See more in Primitive Data Types

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35