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 .
StringBuffer's append() method can handle null args.
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
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
sb.append(null);
with
sb.append(String.valueOf(null));
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
Mohit Agarwal
Ranch Hand
Joined: Mar 30, 2004
Posts: 88
posted
0
Thanks to all of you for clearing the doubt and providing such a lucid explanation .
Bye.
Philip Heller
author
Ranch Hand
Joined: Oct 24, 2000
Posts: 119
posted
0
Oops, Rakesh is right. I meant to say
Nischal Tanna
Ranch Hand
Joined: Aug 19, 2003
Posts: 182
posted
0
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.
Thanks, ~Rakesh
Shweta R Dhaneshwar
Ranch Hand
Joined: May 27, 2005
Posts: 35
posted
0
Thank you all for your efforts and time.Now its clear to me how it works
Chitra AP
Ranch Hand
Joined: May 25, 2005
Posts: 42
posted
0
Hi Swetha,
I didn't get it correctly. Could you please explain? Thanks.