Abhishek Singh
Abhishek Singh

Reputation: 953

Random escape sequences in java

I am playing with escape character(backslash \) in java. When I get the length(number of bytes actually taken to store) of \n or \t, I get 1 and when I get length of \n\t, I get 2, as expected.

My confusion starts when I print:

length of \123  -> 1
length of \177  -> 1
length of \178  -> 2
length of \190  -> 3

How is this happening? If it's related to ASCII or extended-ASCII, then this should change from 164. Another observation is after first three characters it starts counting each char as 1 length, so e.g. \123456 has length 4.

Is it something related to the encoding? I have UTF-8 set into my IDE right now.

This could be a silly question but I don't have detail knowledge of unicode or it's encoding, can someone please explain?

Upvotes: 2

Views: 355

Answers (1)

vmrvictor
vmrvictor

Reputation: 723

When you use \ and a number you are using octal numbers https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.6 , when converting octal 123 to hex It is 53 https://decimaltobinary.pro/Convert_octal_number_123_to_hexadecimal_ , 53 in hex in ASCII is 'S' https://ascii.cl/

as we are on base 8 we can use digits from 0 to 7:

  • 123, all numbers can be treated as octal.

  • 177, all numbers can be treated as octal.

  • 178, 1 and 7 are under 8 can be converted, 8 is outside base 8. for that reason 8 is being taken apart as a character.

  • 190, 1 can be part of base 8, but 9 no so 9 and all digits after him are treated as characters.

  • 123456 we can use ASCII in octal from 0 to 177 (7F), so 123 can be transformed to one character.

Upvotes: 2

Related Questions