1. Octal escape sequence: \ + 1 to 3 digits of 5; range '\000' ~ '\377'
\0: Null character
2. Unicode escape character: \u + four hexadecimal digits; 0 ~65535
\u0000: Null character
3. Special characters: just 3
\": double quote
\': single quote
\\: backslash
4. Control characters: 5
\' single quote character
\\ backslash character
\ r carriage return
\n line feed
\f paper feed
\t horizontal tab
\b backspace
point escape: . ==> u002E
dollar sign escape: $ ==> u0024
power sign escape: ^ ==> u005E
Escape for opening brace: { ==> u007B Escape for opening
bracket: [ ==> u005B Escape for opening
parenthesis: ( ==> u0028 Escape for
vertical bar: | ==> u007C
Escape of right parenthesis:) ==> u0029 Escape
of asterisk: * ==> u002A Escape
of plus sign: + ==> u002B Escape
of question mark: ? ==> u003F Escape
of backslash Meaning: ==> u005C
换行符在不同的系统下有不同的形式:
\n: Mac os下的文本文件换行符
\r: unix下的文本文件换行符
\r\n: windows下的文本文件换行符
下面的程序使用了两个Unicode的转义字符,它们是用其十六进制代码来表示Unicode字符。那么,这个程序会打印什么呢?
Java代码
public class EscapeRout{
public static void main(String[] args){
// \u0022 是双引号的Unicode转义字符
System.out.println("a\u0022.length()
+\u0022b".length());
}
}
public class EscapeRout{
public static void main(String[] args){
// \u0022 是双引号的Unicode转义字符
System.out.println("a\u0022.length()
+\u0022b".length());
}
}
对该程序的一般的分析会认为它应该打印出26,因为在由两个双引号"a\u0022.length()+\u0022b"标识的字符串之间总共有26个字符。
稍微深入一点的分析会认为该程序应该打印16,因为两个Unicode转义字符每一个在源文件中都需要用6个字符来表示,但是它们只表示字符串中 的一个字符。因此这个字符串应该比它的外表看其来要短10个字符。 如果你运行这个程序,就会发现事情远不是这么回事。它打印的既不是26也不是16,而是2。
Java对在字符串字面常量中的Unicode转义字符没有提供任何特殊处理。编译器在将 程序解析成各种符号之前, 先将Unicode转义字符转换成为它们所表示的字符因此,程序中的第一个Unicode转义字符将作为一个单字符字符串字面常量("a")的结束引号, 而第二个Unicode转义字符将作为另一 个单字符字符串字面常量("b")的开始引号。程序打印的是表达式"a".length()+"b".length(),即2。
转载自 http://ljhzzyx.blog.163.com/blog/static/38380312200971415534375/