public class EscapeRout {
public static void main(String...strings){
System.out.println("a/u0022.length() + /u0022b".length());
}
}
这一行会打印什么呢?
2
实质上第三行编译的结果是:
System.out.println(“a”.length() + “b”.length());
如果将这行改成:
System.out.println("a/".length() + /"b".length());
那么打印的结果是:
16
In summary, prefer escape sequences to Unicode escapes in string and character literals. Unicode escapes can be confusing because they are processed so early in the compilation sequence. Do not use Unicode escapes to represent ASCII characters. Inside of string and character literals, use escape sequences; outside of these literals, insert ASCII characters directly into the source file.
本文详细解析了Java中字符串长度计算与Unicode编码的区别,通过实例展示了如何正确使用字符串和字符字面量中的转义序列及Unicode转义,强调避免在非字面量上下文中使用Unicode转义来表示ASCII字符。
1万+

被折叠的 条评论
为什么被折叠?



