How come the following doesn't throw a runtime exception on the first
iteration?
String temp = null; for (int i = 0; i < 3; i++) { temp += "hello"; }
It doesn't seem like you should be able to append anything to a NULL string. The following is printed:
nullhellohellohello
In Java a String, once created, is never modified. On each pass through the loop a new String is created by summing the earlier String with the new "hello", and a pointer to this new String is assigned into the variable temp. The pointer to the earlier String is thus overwritten. Incidentally, that earlier String becomes eligible for garbage collection, since now no variable holds a pointer to it.
On the first pass through the loop the string-addition function turns the value null into a string "null", allowing it to participate in the addition.
Rich Hammer
_______________________________________________ Juglist mailing list [EMAIL PROTECTED] http://trijug.org/mailman/listinfo/juglist_trijug.org
