📜  带下划线的Java数字字面量

📅  最后修改于: 2022-05-13 01:55:11.445000             🧑  作者: Mango

带下划线的Java数字字面量

JDK 7 引入了一个新特性,它允许使用下划线字符编写数字字面量。数字字面量被打破以提高可读性。此功能用于分隔数字字面量中的一组数字,可以提高源代码的可读性。在使用数字字面量之前,程序员必须遵循一些规则,例如

1. 我们不应该在整数之前或之后使用下划线。

int p = _10; // Error, this is an identifier, not a numeric literal  
int p = 10_; // Error, cannot put underscores at the end of a number  

2. 我们不应该在浮点字面量中的小数点之前或之后使用下划线。

float a = 10._0f; // Error, cannot put underscores adjacent to a decimal point  
float a = 10_.0f; // Error, cannot put underscores adjacent to a decimal point  

3.我们不应该分别在long和float整数中使用before L或F后缀。

long a = 10_100_00_L; // Error, cannot put underscores prior to an L suffix  
float a = 10_100_00_F; // Error, cannot put underscores prior to an F suffix  

4. 我们不能在需要字符串数字的位置使用下划线。

例子

Java
// Java program to demonstrate Numeric Literals with
// Underscore
  
public class Example {
    public static void main(String args[])
    {
        // Underscore in integral literal
        int a = 7_7;
        System.out.println("The value of a is=" + a);
  
        // Underscore in floating literal
        double p = 11.239_67_45;
        System.out.println("The value of p is=" + p);
  
        float q = 16.45_56f;
        System.out.println("The value of q is=" + q);
  
        // Underscore in binary literal
        int c = 0B01_01;
        System.out.println("c = " + c);
  
        // Underscore in hexadecimal literal
        int d = 0x2_2;
        System.out.println("d = " + d);
  
        // Underscore in octal literal
        int e = 02_3;
        System.out.println("e = " + e);
    }
}


输出
The value of a is=77
The value of p is=11.2396745
The value of q is=16.4556
c = 5
d = 34
e = 19