在Java的数字文本中使用下划线

JDK 7引入了一个新特性,允许使用下划线字符编写数字文本。基本上,它们是为了提高可读性而被打破的。此功能使我们能够以数字文字分隔数字组,从而提高代码的可读性。例如,如果我们的代码包含许多数字,我们可以使用下划线字符将数字分成三组,类似于使用逗号或空格等标点符号作为分隔符。让我们用一个例子进一步探讨

null

实例

JAVA

// Java Program to Illustrate Different Ways of Usage
// of Underscore in Numeric Literals
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
throws java.lang.Exception
{
// Declaring and initializing values of
// integer, long, float and double datatype
// Integer literal
int inum = 1_00_00_000;
// Long literal
long lnum = 1_00_00_000;
// Float literal
float fnum = 2 .10_001F;
// Double literal
double dnum = 2 .10_12_001;
// Printing and displaying values on console
System.out.println( "inum:" + inum);
System.out.println( "lnum:" + lnum);
System.out.println( "fnum:" + fnum);
System.out.println( "dnum:" + dnum);
}
}


输出

inum:10000000lnum:10000000fnum:2.10001dnum:2.1012001

本文由 闪烁泰吉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以写一篇文章,然后将文章邮寄给评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享