Which lines of code here use numeric underscores with literals properly?

Discussions

News: Which lines of code here use numeric underscores with literals properly?

  1. So, which of the following attempts at leveraging the new Java 7 feature of using numeric underscores with literals actually uses the syntax properly?

    int overflow = 0b1010_1010_1010_1010_1010_1010_1010_1011;
    long bow = 0b1__01010101__01010101__01010101__01010111L;
    int x_ = 1_;
    int _1 = 12345;
    int x = _1;
    double pious2 = 3._14;
    float myBoat1 = 321.123_F;
    float myBoat2 = 321.1_23F;
    int hexed = 0_x_BABE;

    Rules for using binary notation

    Well, it's easier figuring out what works and what doesn't if you stick to the following four rules:

    1. Underscores can't go at the beginning or the end of a number.
    2. You can't use an underscore on either side of a decimal.
    3. The underscore cannot go before an identifying suffix such as F, D or L
    4. You can't put an underscore before or after the binary or hexadecimal identifiers b and x.

    Expanding on Java 7

    We've got a simply little tutorial on the topic if you want to brush up on the ins and outs of this new Java 7 feature, which expands on some of the other concepts that were discussed in the article on another new Java 7 feature, binary notation.

    How to use underscores with numeric literals in Java 7
    What you need to know about Java 7 and binary notation

     

    Related Books and Resources

    What's New in Java 7? by Madhusudhan Konda
    Java 7: A Beginner's Tutorial by Budi Kurniawan
    OCP Java SE 7 Programmer Study Guide 
    by Kathy Sierra
    Java 7 and Polyglot Programming on the JVM 
    by Benjamin J Evans 

    Threaded Messages (4)

  2. int overflow = 0b1010_1010_1010_1010_1010_1010_1010_1011;  // Valid.

    long bow = 0b1__01010101__01010101__01010101__01010111L; // Valid.

    int x_ = 1_; //Invalid.

    int _1 = 12345; // Valid.

    int x = _1; // Invalid.

    double pious2 = 3._14; // Invalid as having '_'  before decimal point.

    float myBoat1 = 321.123_F;  // Invalid. should not have '_' prior to L or F.

    float myBoat2 = 321.1_23F;  // Vallid.

    int hexed = 0_x_BABE; // Invalid.

  3. Well done...[ Go to top ]

    Looking for a little light entertainment on Christmas? :)

    Well done.

  4. Rules seem confusing.[ Go to top ]

    The four rules seem slightly confusing to me (not helping that "point" is missing after "decimal" in #2). After reading them, I think they could be condensed to just:

    One or more underscores may be added between any two adjacent digits of a number.

    Now, after the fact, I went and read the Oracle description, which also starts out by saying "In numeric literals, underscores are allowed as separators between digits." and the grammar also seems to say the same thing.

  5. Rules seem confusing.[ Go to top ]

    The four rules seem slightly confusing to me (not helping that "point" is missing after "decimal" in #2). After reading them, I think they could be condensed to just:

    One or more underscores may be added between any two adjacent digits of a number.

    Now, after the fact, I went and read the Oracle description, which also starts out by saying "In numeric literals, underscores are allowed as separators between digits." and the grammar also seems to say the same thing.