简体   繁体   English

使用转义序列在java中打印八进制字符

[英]Printing Octal characters in java using escape sequences

Please explain the below code 请解释下面的代码

public class Example{
   public static void main(String[] args)
   {
      int i[]={9};
       System.out.println("\700");
   }
}

Please don't say me that the octal value should be less than 377. I know it already but when I run the above program, I get the output as 80 . 请不要说我的八进制值应该小于377.我已经知道但是当我运行上面的程序时,我得到输出为80 I want to know why its happening so? 我想知道它为什么会这样发生?

Please give a clear explanation. 请说清楚。 Thank you 谢谢

Basically, you've got two characters there: '\\70' and '0' . 基本上,你有两个角色: '\\70''0'

The escape sequence for octals is documented in the JLS as: octals的转义序列在JLS中记录为:

OctalEscape:
\ OctalDigit 
\ OctalDigit OctalDigit 
\ ZeroToThree OctalDigit OctalDigit 

The last of these doesn't apply in your case, as '7' isn't in ZeroToThree, but both '7' and '0' are octal digits, so it matches the second pattern. 最后一个不适用于您的情况,因为'7'不在ZeroToThree中,但'7'和'0'都是八进制数字,因此它匹配第二个模式。

So, now we just need to know why '\\70' is '8'... and that's because octal 70 is decimal 56 or hex 38, which is the UTF-16 code unit for '8' . 所以,现在我们只需要知道为什么'\\70'是'8'......那是因为八进制70是十进制56或十六进制38,这是'8'UTF-16代码单元

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM