歡迎您光臨本站 註冊首頁

Java註釋代碼執行方法解析

←手機掃碼閱讀     ljg58026 @ 2020-05-13 , reply:0

直接上代碼:
@Test public void testUnicode() { String a = "Hello"; // u000d a="world"; System.out.println(a); // u000a a="hello world!"; System.out.println(a); }
猜一猜,最後會輸出什麼?
world
hello world!
是的,沒看錯,那二行看似“註釋掉的代碼”,被執行了!
原因:java編譯器會處理unicode字符,u000d以及u000a 正好對應“ ”回車、“
”換行,經過編譯器處理後,等效於下面的代碼:
@Test public void testUnicode() { String a = "Hello"; // a="world"; System.out.println(a); // a="hello world!"; System.out.println(a); }
個人建議:正式項目中,嚴禁在註釋中使用這一類unicode字符,否則如果利用這個特性乾點壞事,埋個坑,基本上很難發現!


[ljg58026 ] Java註釋代碼執行方法解析已經有243次圍觀

http://coctec.com/docs/java/show-post-234281.html