java 转义字符加数字,什么是字符文字中的转义数字的Java语义,例如。 '\15'?

Please explain what, exactly, happens when the following sections of code are executed:

int a='\15';

System.out.println(a);

this prints out 13;

int a='\25';

System.out.println(a);

this prints out 21;

int a='\100';

System.out.println(a);

this prints out 64.

解决方案

You have assigned a character literal, which is delimited by single quotes, eg 'a' (as distinct from a String literal, which is delimited by double quotes, eg "a") to an int variable. Java does an automatic widening cast from the 16-bit unsigned char to the 32-bit signed int.

However, when a character literal is a backslash followed by 1-3 digits, it is an octal (base/radix 8) representation of the character. Thus:

\15 = 1×8 + 5 = 13 (a carriage return; same as '\r')

\25 = 2×8 + 5 = 21 (a NAK char - negative acknowledgement)

\100 = 1×64 + 0×8 + 0 = 64 (the @ symbol; same as '@')

For more info on character literals and escape sequences, see JLS sections:

Quoting the BNF from 3.10.6:

OctalEscape:

\ OctalDigit

\ OctalDigit OctalDigit

\ ZeroToThree OctalDigit OctalDigit

OctalDigit: one of

0 1 2 3 4 5 6 7

ZeroToThree: one of

0 1 2 3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值