Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gwt null加null有时为0,有时为“0”;nullnull";_Gwt - Fatal编程技术网

Gwt null加null有时为0,有时为“0”;nullnull";

Gwt null加null有时为0,有时为“0”;nullnull";,gwt,Gwt,在编译模式下 String s1 = null; String s2 = null; s2 = s1 + s2; Window.alert("null + null = " + s2); 结果: "null + null = nullnull" 但这段代码: public String getValue(DocListsInfo object) { String s1 = object.getUrlForApplication(); Window.alert("2: " +

在编译模式下

String s1 = null;
String s2 = null;
s2 = s1 + s2;
Window.alert("null + null = " + s2);
结果:

"null + null = nullnull"
但这段代码:

public String getValue(DocListsInfo object) {
    String s1 = object.getUrlForApplication();
    Window.alert("2: " + s1);
    String s2 = object.getEodLink();
    Window.alert("3: " + s2);
    String s3 = s1 + s2;
    Window.alert("4: " + s3);
    return s3;
}

=>
// compiled javascript:
function NUb(a){var b,c,d;b=a.q;$wnd.alert('2: '+b);c=a.g;$wnd.alert('3: '+c);d=b+c;$wnd.alert('4: '+d);return d}

如果两个getter都返回null,则返回0。

我对GWT编译器内部工作原理的了解非常有限,但我想我可能知道发生了什么

首先,这两个片段之间有什么区别?关于第一个的每一个细节在编译时都是已知的(两个
String
s都是
null
),而我们对第二个知之甚少

现在,Java中的
null+null
是什么?它是可以读取的
null

然而,JavaScript中的
null+null
是什么?它是
0
,您只需在浏览器中测试即可


现在我想发生的事情如下:让我们从第二段代码开始。它被编译成JavaScript,JavaScript随后将确定
s1
s2
是基于
对象的。然后它将尝试添加/连接它们,结果是
0
。好吧,这没关系,因为这是您在使用JavaScript时所期望的(如果您只懂Java,就不会这么多)

第一个代码片段(我猜是这样)似乎是通过编译器的前/后处理步骤优化的(因为我们已经知道它们都是
null
)。因此,JavaScript所看到的(来自编译器的)都是一个常量



也许其他人可以对我猜测的步骤提供更多信息。

什么是
getUrlForApplication()
getEodLink()
方法的返回类型?第一个代码段的编译版本是什么样的?它具有常量“null+null=nullnull”: