Printing octal characters in java using escape sequences

Please explain the code below.

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

Please do not tell me that the octal value should be less than 377. I already know this, but when I run the program above, I get the output as 80 . I want to know why this is happening like this?

Please provide a clear explanation. thank you

+6
source share
1 answer

Basically, you have two characters: '\70' and '0' .

The escape sequence for octal is documented in JLS as:

 OctalEscape: \ OctalDigit \ OctalDigit OctalDigit \ ZeroToThree OctalDigit OctalDigit 

The last one does not apply in your case, since β€œ7” is not in ZeroToThree, but both β€œ7” and β€œ0” are octal digits, so it matches the second pattern.

So now we just need to know why '\70' is "8" ... and that since octal 70 is decimal 56 or hexadecimal 38, what is the UTF-16 code block for "8" .

+7
source

Source: https://habr.com/ru/post/984235/


All Articles