0

In compiled mode.

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

result:

"null + null = nullnull"

But this piece of code:

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}

returns 0 if both getters return null.

basin
  • 3,949
  • 2
  • 27
  • 63

1 Answers1

1

My insight into the inner workings of the GWT compiler is quite limited, but I think I might have an idea what's going on.

First of all, what's the difference between the two snippets? Every detail about the first one is known at compile time (both Strings are null) whereas we don't know much about the second one.

Now, what is null + null in Java? It's nullnull as can be read here.

However, what's null + null in JavaScript? It's 0 as you can simply test in your browser.


Now what I think is going on is the following: Let's start with your second code snippet. It is compiled to JavaScript, where JavaScript will then determine what s1 and s2 are based on object. It will then try to add/concatenate them and the result is 0. Well, that's ok, since that's what you would expect when you use JavaScript (well, not so much if you just know Java).

The first code snippet (and here comes my guess) seems to be optimized by a pre-/post-processing step of the compiler (since we already know they are both null). Consequently, all that JavaScript sees (what came out of the compiler) is a constant.


Maybe someone else can shed some more light on the step I guessed.

Community
  • 1
  • 1
Baz
  • 36,440
  • 11
  • 68
  • 94