Using underscore in Numeric Literals in Java


Followig is the code showing how to use underscore in numeric literals in Java −

Example

 Live Demo

public class Demo{
   public static void main (String[] args) throws java.lang.Exception{
      int my_num_1 = 6_78_00_120;
      System.out.println("The number is : " + my_num_1);
      long my_num_2 = 2_00_11_001;
      System.out.println("The number is : " + my_num_2);
      float my_num_3 = 4.01_981F;
      System.out.println("The number is : " + my_num_3);
      double my_num_4 = 12.89_46_061;
      System.out.println("The number is : " + my_num_4);
   }
}

Output

The number is : 67800120
The number is : 20011001
The number is : 4.01981
The number is : 12.8946061

A class named Demo contains the main function which uses various formats of integer, long, floating point values and double values to display data with the help of underscotres. This is assigned to a variable each, that is printed on the console.

Updated on: 09-Jul-2020

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements