请解释下面的代码
public class Example{
public static void main(String[] args)
{
int i[]={9};
System.out.println("\700");
}
}
请不要告诉我八进制值应该小于 377。我已经知道了,但是当我运行上面的程序时,我得到的输出是 80。我想知道为什么会这样?
请给出明确的解释。谢谢
最佳答案
基本上,您有两个字符:'\70'
和 '0'
。
八进制的转义序列是documented in the JLS作为:
OctalEscape:
\ OctalDigit
\ OctalDigit OctalDigit
\ ZeroToThree OctalDigit OctalDigit
最后一个不适用于您的情况,因为“7”不在 ZeroToThree 中,但“7”和“0”都是八进制数字,因此它与第二个模式匹配。
所以,现在我们只需要知道为什么 '\70'
是 '8'...那是因为八进制的 70 是十进制的 56 或十六进制的 38,即 UTF-16 code unit for '8' .
关于java - 使用转义序列在java中打印八进制字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29258506/