gpt4 book ai didi

java - 为什么 1_2_3_4 在 java 中是有效的整数文字?

转载 作者:行者123 更新时间:2023-12-01 18:14:02 24 4
gpt4 key购买 nike

为什么下面的方法有效?

int a=1_2_3_4;
System.out.println(a); // 1234

最佳答案

数字文字在 JLS 3.10.1 中指定.

A decimal numeral is either the single ASCII digit 0, representing the integer zero, or consists of an ASCII digit from 1 to 9 optionally followed by one or more ASCII digits from 0 to 9 interspersed with underscores, representing a positive integer.

[...]

A hexadecimal numeral consists of the leading ASCII characters 0x or 0X followed by one or more ASCII hexadecimal digits interspersed with underscores, and can represent a positive, zero, or negative integer.

[...]

An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.

[...]

A binary numeral consists of the leading ASCII characters 0b or 0B followed by one or more of the ASCII digits 0 or 1 interspersed with underscores, and can represent a positive, zero, or negative integer.

如果您问为什么下划线不必以三位数字为一组来表示十进制文字,不同的文化对数字进行不同的分组 - 当然对于十六进制和二进制文字,根据用途,您可能需要所有类型不同明显的分组。

关于java - 为什么 1_2_3_4 在 java 中是有效的整数文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30815964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com