日期:2014-05-20  浏览次数:20632 次

请求精通java高手来回答问题
原题是在百度贴吧一个高手提的编译器工作原理问题如下:
我们知道System.out.print();这样是不对的,因为没有重载无参数的方法。因此实参为'\u000a'或'\u000d'是会编译错误的,但是'\n'和'\r'都没错,这是为什么?另外'\u0000'为“空”,但为什么System.out.print(‘\u0000’);没错?
而一位回答者的经典回答如下:
编译的时候编译器会把源代码中所有的unicode代码转换过来,然后再做语法分析   System.out.println('\u000a');被编译器认为是
1.System.out.print(" 
2.a"); 
我觉得  System.out.println('\u000a');被编译器认为的应该是
1.System.out.print(‘ 
2’);
不知道我对这句话“编译器会把源代码中所有的unicode代码转换过来,然后再做语法分析”的理解有误没。

------解决方案--------------------

这个问题跟 print() 方法的 overloading 没有任何关系。



问题出在 character literal 本身,JLS里面写得很清楚:

(JLS 7 - 3.10.4 Character Literals)

It is a compile-time error for a line terminator (§3.4) to appear after the opening
' and before the closing '.

如果你不调用任何方法,下面这样写照样不能通过编译:


char c = '\u000a';


提示为 "illegal line end in character literal"