At compile time, 
        foo+=bar 
becomes
        foo = new StringBuffer().append(foo).append(bar).toString();

Thus, the observed output:

>       nullhellohellohello

(One of the dangers of syntactic sugar, IMHO.)

HTH,
Ken


> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Tony Spencer
> Sent: Friday, October 29, 2004 4:38 PM
> To: 'Research Triangle Java User's Group mailing list.'
> Subject: [Juglist] Why doesn't this throw an exception?
> 
> 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
> 
> 
> _______________________________________________
> Juglist mailing list
> [EMAIL PROTECTED]
> http://trijug.org/mailman/listinfo/juglist_trijug.org
> 

_______________________________________________
Juglist mailing list
[EMAIL PROTECTED]
http://trijug.org/mailman/listinfo/juglist_trijug.org

Reply via email to