A new feature was introduced by JDK 7 which allows to write numeric literals using the underscore character. Numeric literals are broken to enhance the readability.
This feature enables us to separate groups of digits in numeric literals, which improves readability of code. For instance, if our code contains numbers with many digits, we can use an underscore character to separate digits in groups of three, similar to how we would use a punctuation mark like a comma, or a space, as a separator.
The following example shows different ways we can use underscore in numeric literals:
// Java program to demonstrate that we can use underscore // in numeric literals class Test { public static void main (String[] args) throws java.lang.Exception { int inum = 1_00_00_000; System.out.println("inum:" + inum); long lnum = 1_00_00_000; System.out.println("lnum:" + lnum); float fnum = 2.10_001F; System.out.println("fnum:" + fnum); double dnum = 2.10_12_001; System.out.println("dnum:" + dnum); } } |
Output:
inum: 10000000 lnum: 10000000 fnum: 2.10001 dnum: 2.1012001
This article is contributed by Twinkle Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. 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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Using underscore in Numeric Literals in Java
- Remove uppercase, lowercase, special, numeric, and non-numeric characters from a String
- Literals in Java
- Java Numeric Promotion in Conditional Expression
- Using _ (underscore) as variable name in Java
- Accessing Grandparent’s member in Java using super
- Producer-Consumer solution using threads in Java
- Copying file using FileStreams in Java
- Swap two Strings without using third user defined variable in Java
- File handling in Java using FileWriter and FileReader
- Simple Calculator using TCP in Java
- Using final with Inheritance in Java
- Calling an External Program in Java using Process and Runtime
- Creating Frames using Swings in Java
- Java program to check palindrome (using library methods)
- Mutation Testing in Java (Using Jumble)
- How to rename all files of a folder using Java?
- Delete a file using Java
- Moving a file from one directory to another using Java
- Send email using Java Program

