Skip to content
Related Articles

Related Articles

Using underscore in Numeric Literals in Java
  • Difficulty Level : Hard
  • Last Updated : 06 Sep, 2017

From 1.7v onwards we can use ‘_’ under score symbols between digits of numeric literals. You can place underscores only between digits; you cannot place underscores in the following places:

  • At the beginning or end of a number
  • Adjacent to a decimal point in a floating point literal
  • Prior to an F or L suffix
  • In positions where a string of digits is expected
  • We can use under score symbols only between the digits if we are using else we will get compile time error.

Valid examples:

Input : int i = 12_34_56; 
Output : 123456

Input : double db = 1_2_3.4_5_6
Output : 123.456

Some invalid examples:

int i = _12345; // Invalid; This
is an identifier, not a numeric literal

double db = 123._456; // Invalid; cannot put 
underscores adjacent to a decimal point

double db 123_.456_; // Invalid; cannot put
underscores at the end of a number 

The main advantage of this approach is readability of the code will be improved. At the time of compilation these under score symbols will be removed automatically. We can use more than one under score symbols also between the digits too. For example, following is a valid numeric literal

int x4 = 5_______2;        // OK (decimal literal)

// Java program to illustrate 
// using underscore in Numeric Literals
class UnderScoreSymbols
    public static void main(String[] args)
        int i = 12_34_5_6;
        double db = 1_23.45_6;
        int x4 = 5_______2;  
        System.out.println("i = " + i );
        System.out.println("db = " + db );
        System.out.println("x4 = " + x4 );


i = 123456
db = 123.456
x4 = 52

This article is contributed by Shivakant jaiswal. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :