Skip to main content

JDK7 new numeric litteral formats

Posted by forax on September 24, 2009 at 5:07 AM PDT

The second small change from coin project, new numeric litterals has been integrated to jdk7/tl workspace
and will be soon promoted into jdk7 main workspace.

The patch introduces two new notations for numeric litterals:

  1. Binary litteral

    Litteral coded in binary starting with prefix 0b.
    This prefix can be prefixed by minus for negative number.
         0b11 == 3
         -0b11 == -3

    More examples here.

  2. Underscores in litterals

    Allow to use underscore ('_') to separate digits in litterals.
    Underscore can be used anywhere between two digits.
         long aBankAccountBalance = 1_000_000_000;
         long aVisaCardNumber = 1234_1234_1234_1234;
         long aFrenchPhoneNumber = 01_60_95_77_33;

Of course, these two new syntax can be mixed:

    int value = 0b11111111_00000000_11111111_00000000;



Related Topics >>



Very interesting! I do like the binary format - at long last! I usually write my numeric literals as 1000 * 1000 * 1000. This of course won't work when the number is 323942384. One question though: would octals not be supported anymore? Surely the French phone number of long aFrenchPhoneNumber = 01_60_95_77_33; should be octal, as it starts with 0 ? Heinz ( - The Java Specialists' Newsletter)

Cool, what about type suffix on constantes ?

Bonjour Rémi, I realy like the Ob syntax :) Could it be possible to have another small one on the same area : "ordinal type suffix" on constants for all the simple types. You know L indicates long as by default ordinal type is int, but there is no direct way to indicate the type on byte and short So at this time, this does not compile : public void add2( ){ add(0); } public void add( short value){ } Why not permit S for short and B for byte at the end of a constant as well ? But with the suffix indicating the type, if I write : public void add2( ){ add(0S); } public void add( short value){ } A+ JB

Merveilleuse nouvelle !

Super Rémi merci !
And what about the suffix type specifiers ? y for byte and s for short, as there is already l for long.
0xFFy == (byte)-1
-0x1y == (byte)-1
325s == (short)325

Are those already included or planed for a future inclusion ?

efl, The byte and short


The byte and short suffixes are still under consideration for exactly how it is done. But yes they will be in some time in the (JDK7) future and in some form, probably per your examples. I am holding it up, due to a lack of time resource due to a family crisis. It will happen.

Very good to know!

Keep 'em coming. More grease to your elbows!