1. 程式人生 > >解析Unicode轉義序列帶來的問題

解析Unicode轉義序列帶來的問題

Unicode轉義序列的解析是發生在程式碼編譯之前,編譯器機械的將\u樣式的程式碼文字轉義,即使是註釋以及非正常程式碼,對此步驟來說也沒有區別

導致下面的情況:

 1 public class Test {
 2     public static void main(String[] args) {
 3         System.out.println("\u0022+\u0022");//""+""=null
 4         //\u000a System.out.println("annotation !");//print success!
 5         //\n System.out.println("annotation !");
//print success! 6 7 } 8 } 9 Output: 10 11 annotation !

由於機械轉義,"\u0022+\u0022"實際上是""+""即兩個空字串相加,所以列印為空

註釋中的\u000a被轉移成\n換行符,所以其後的print程式碼得以執行

而普通的\n轉義序列卻不會產生問題