This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt.......null? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt.......null?" Watch "Doubt.......null?" New topic
Author

Doubt.......null?

Shweta R Dhaneshwar
Ranch Hand

Joined: May 27, 2005
Posts: 35
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 .


Shweta Dhaneshwar.<br />SCJP 1.4 90%
Siddhartha Bhattacharya
Ranch Hand

Joined: Jul 02, 2002
Posts: 36
operator overloading. string concatenation.
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
The statement



is equivalent to



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".
rakesh damkondwar
Greenhorn

Joined: Sep 17, 2004
Posts: 9
Hi Philip,
The following code,

StringBuffer sb = new StringBuffer();
sb.append(s1);
sb.append(null);
s2 = sb.toString();

should give compile time error "saying ambigutiy in calling method .append(char []) on StringBuffer".

Thanks,
~Rakesh
Mohit Agarwal
Ranch Hand

Joined: Mar 30, 2004
Posts: 88
Hi ,

Any one please clarify this .
I feel that adding a null reference with another null reference should return a null.

I am perplexed .

Tx in advance ,

Mohit .
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
What's the hard bit to understand?
If in doubt, consult the SDK source - it becomes quite evident.
Particularly the String.valueOf(Object) method.


Tony Morris
Java Q&A (FAQ, Trivia)
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Concatenation
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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
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
Oops, Rakesh is right. I meant to say
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
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 .



Always remember Object+String is always a String


Thnx
Nik Raut
Greenhorn

Joined: May 23, 2005
Posts: 22

That is the most simple and efficient way to put it...

object + String = String


To be yourself is the best thing to be.
rakesh damkondwar
Greenhorn

Joined: Sep 17, 2004
Posts: 9
Hi Phillip,

Phillip - "I meant to say

StringBuffer sb = new StringBuffer();
sb.append(s1);
sb.append((Object)null);
s2 = sb.toString(); "

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
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
Hi Swetha,

I didn't get it correctly. Could you please explain? Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt.......null?