This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Consider this code,I thought that this code won't compile since I was wondering how a null can be added to a null.This code compiles just fine.Next I thought that may be this will give a NullPointerException when run....but to my surprise the o/p is "nullnull".please explain .
Hey All, This is the sequence how the code runs: 1) It does a String.valueOf(null)--> If the null is passed as an argument, literal string "null" is returned 2) Then new StringBuffer("null") is called 3) Finally on the above created StringBuffer object append("abc") method is run. The final output is a SringBuffer object having contents as "nullabc".
4) At the end toString() is called to return a new String object.
So, a += "abc" ==> a = (new StringBuffer((String.valueOf(null)).append("abc")).toString();
Hope u got this
But there are some slight changes the way the program runs depending on the variables, --------------------------- Now if u have
The way it runs is like this a = (new StringBuffer((String.valueOf(a)).append(String.valueOf(b))).toString();
Now if u have something like this
The simplified statement is: a = (new StringBuffer("abc").append(b)).toString();
----------------------------------------------------------- In the method append() of StringBuffer u have something like this:
Hopw it clears your doubt
Please also look here, i have copied from this post
[Rakesh]: ...should give compile time error "saying ambigutiy in calling method .append(char ) on StringBuffer".
A minor error in the code provided - StringBuffer has many, many overloads, and a null argument doesn't have enough info for the compiler to tell which one is meant. Just replace
or insert a cast to (Object) or String) remove the ambiguity.
[Mohit]: I feel that adding a null reference with another null reference should return a null.
That's not what happens. Since one of the operands has the declared type String, the + operator indicates string concatenation. The rules for string concatenation are described in JLS 15.18.1. The relevant part here is: "If the reference is null, it is converted to the string 'null'"
"I'm not back." - Bill Harding, Twister
Joined: Mar 30, 2004
Thanks to all of you for clearing the doubt and providing such a lucid explanation .
Originally posted by Shweta R Dhaneshwar: Consider this code,I thought that this code won't compile since I was wondering how a null can be added to a null.This code compiles just fine.Next I thought that may be this will give a NullPointerException when run....but to my surprise the o/p is "nullnull".please explain .
I could not understand one thing in your above code, does the compiler explicitely type casts null to Object when calling append() method on StringBuffer. If not then how does above case would be handled by compiler, as when i try to pass null to append() method it throws compile time error.
Shweta R Dhaneshwar
Joined: May 27, 2005
Thank you all for your efforts and time.Now its clear to me how it works