Java中数字里面混合有下划线10_000 代表什么意思?

复制代码
public static void main(String[] args) {
        int a = 10_000;
        System.out.println(a);  // 10000
    }

java 7 的 特性 :

复制代码
https://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html

程序中的数字可以使用下划线来进行分割(_)以便于为程序提供更好的可读性。

JDK 在编译的时候,将会对数字中间的下划线进行处理,替换掉下划线。

Underscores in Numeric Literals

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

The following example shows other ways you can use the underscore in numeric literals:

复制代码
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 	3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;

You can place underscores only between digits; you cannot place underscores in the following places:

  • At the beginning or end of a number
  • Adjacent to a decimal point in a floating point literal
  • Prior to an F or L suffix
  • In positions where a string of digits is expected

The following examples demonstrate valid and invalid underscore placements (which are highlighted) in numeric literals:

复制代码
float pi1 = 3_.1415F;      // Invalid; cannot put underscores adjacent to a decimal point
float pi2 = 3._1415F;      // Invalid; cannot put underscores adjacent to a decimal point
long socialSecurityNumber1
  = 999_99_9999_L;         // Invalid; cannot put underscores prior to an L suffix

int x1 = _52;              // This is an identifier, not a numeric literal
int x2 = 5_2;              // OK (decimal literal)
int x3 = 52_;              // Invalid; cannot put underscores at the end of a literal
int x4 = 5_______2;        // OK (decimal literal)

int x5 = 0_x52;            // Invalid; cannot put underscores in the 0x radix prefix
int x6 = 0x_52;            // Invalid; cannot put underscores at the beginning of a number
int x7 = 0x5_2;            // OK (hexadecimal literal)
int x8 = 0x52_;            // Invalid; cannot put underscores at the end of a number

int x9 = 0_52;             // OK (octal literal)
int x10 = 05_2;            // OK (octal literal)
int x11 = 052_;            // Invalid; cannot put underscores at the end of a number

中文

数字文字中的下划线

在 Java SE 7 及更高版本中,任意数量的下划线字符 ( _) 可以出现在数字文字中数字之间的任何位置。例如,此功能使您能够分隔数字文字中的数字组,这可以提高代码的可读性。

例如,如果您的代码包含许多数字,则可以使用下划线字符将三组数字分隔开,类似于使用逗号或空格等标点符号作为分隔符。

以下示例显示了在数字文本中使用下划线的其他方法:

复制代码
长信用卡号 = 1234_5678_9012_3456L;
长社会保障号码 = 999_99_9999L;
浮点数 pi = 3.14_15F;
长十六进制字节 = 0xFF_EC_DE_5E;
长十六进制字= 0xCAFE_BABE;
长 maxLong = 0x7fff_ffff_ffff_ffffL;
字节nybbles = 0b0010_0101;
长字节= 0b11010010_01101001_10010100_10010010;

只能在数字之间放置下划线;不能在以下位置放置下划线:

  • 在数字的开头或结尾
  • 浮点数字面值中的小数点附近
  • F在orL后缀 之前
  • 在需要一串数字的位置

以下示例演示了数字文字中有效和无效的下划线位置(突出显示):

复制代码
浮点数 pi1 = 3_.1415F; // 无效的;不能在小数点旁边放置下划线
浮点数 pi2 = 3._1415F; // 无效的;不能在小数点旁边放置下划线
长社会保障号码1
= 999_99_9999_L; // 无效的;不能在 L 后缀之前添加下划线

int x1 = _52; // 这是一个标识符,而不是数字文字
int x2 = 5_2; // OK(十进制文字)
int x3 = 52_; // 无效的;不能在文字末尾添加下划线
整数 x4 = 5_______2; // OK(十进制文字)

int x5 = 0_x52; // 无效的;不能在 0x 基数前缀中添加下划线
int x6 = 0x_52; // 无效的;不能在数字开头添加下划线
int x7 = 0x5_2; // OK(十六进制文字)
int x8 = 0x52_; // 无效的;不能在数字末尾添加下划线

int x9 = 0_52; // OK(八进制文字)
int x10 = 05_2; // OK(八进制文字)
int x11 = 052_; // 无效的;不能在数字末尾添加下划线
相关推荐
程序员黑豆17 分钟前
鸿蒙开发:RelativeContainer相对布局组件实战——实现5个文本组件对齐
前端·华为·harmonyos
环境栈笔记24 分钟前
高性价比指纹浏览器推荐与选型:如何对照价格和实际可用功能筛选候选
前端·人工智能·后端·自动化
小宋102133 分钟前
不用学算法也能玩转 AI?普通 Java 程序员转 AI 应用开发路线
java·人工智能·重构
hyf32663338 分钟前
泛程序哪有想象中难!小白跟着走一遍就全懂
前端·爬虫·搜索引擎·seo·蜘蛛池
码农学院1 小时前
React公众号开发:母婴用品会员积分体系技术方案
前端·react.js·前端框架
我是唐青枫1 小时前
Java Jetty 实战详解:从嵌入式 HTTP 服务到 Spring Boot 容器替换
java
带刺的坐椅1 小时前
当所有人都在用 TS/Python 写 Agent,我们为什么坚持 Java
java·ai·solon·codex·opencode·soloncode
小王师傅661 小时前
英语学习记
java·学习
路光.1 小时前
Vue2升级Vue3处理原 UMD/CommonJS 打包文件,不是标准 ESM
前端·javascript·vue.js·vue3
黄华SJ520it1 小时前
门店预约系统开发:提升服务行业数字化转型
前端·数据库·系统开发