This is a Java Program to Use Underscores in Numeric Literal. A literal is an explicit value.
For example : int number=123;
The value 123 is a literal. Since it’s a normal base-10 value, we might say it is a decimal literal.
For example : int number=123;
The value 123 is a literal. Since it’s a normal base-10 value, we might say it is a decimal literal.
We have used underscore at various positions in different types of datatype values and identified which of them are valid and which are not.
Here is the source code of the Java Program to Use Underscores in Numeric Literal. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Underscore_Numeric
{
public static void main(String[] args)
{
// int a1 = _52; Invalid; cannot put underscores at the start of a literal
int a2 = 5_2; // OK (decimal literal)
System.out.println(a2);
// int a3 = 52_; Invalid; cannot put underscores at the end of a literal
int a4 = 5_______2; // OK (decimal literal)
System.out.println(a4);
// int a5 = 0_x52; Invalid; cannot put underscores in the 0x radix prefix
// int a6 = 0x_52; Invalid; cannot put underscores at the beginning of a number
int a7 = 0x5_2; // OK (hexadecimal literal)
System.out.println(a7);
// int a8 = 0x52_; Invalid; cannot put underscores at the end of a number
int a9 = 0_52; // OK (octal literal)
System.out.println(a9);
int a10 = 05_2; // OK (octal literal)
System.out.println(a10);
// int a11 = 052_; Invalid; cannot put underscores at the end of a number
// float b1 = 3_.1415F; Invalid; cannot put underscores adjacent to a decimal point
// float b2 = 3._1415F; Invalid; cannot put underscores adjacent to a decimal point
float b3 = 3.1_415F; // OK (float literal)
System.out.println(b3);
// long c1 = 9999_L; Invalid; cannot put underscores prior to an L suffix
long c2 = 99_99L; // OK (long literal)
System.out.println(c2);
}
}
Output:
$ javac Underscore_Numeric.java $ java Underscore_Numeric 52 52 82 42 42 3.1415 9999
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Programming Books
- Apply for Java Internship
- Practice Information Technology MCQs
- Practice Programming MCQs
- Check Java Books