Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt.......null?

 
Shweta R Dhaneshwar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Siddhartha Bhattacharya
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
operator overloading. string concatenation.
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The statement



is equivalent to



StringBuffer's append() method can handle null args.
 
rakesh damkondwar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the hard bit to understand?
If in doubt, consult the SDK source - it becomes quite evident.
Particularly the String.valueOf(Object) method.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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'"
 
Mohit Agarwal
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you for clearing the doubt and providing such a lucid explanation .

Bye.
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, Rakesh is right. I meant to say
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nik Raut
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the most simple and efficient way to put it...

object + String = String
 
rakesh damkondwar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your efforts and time.Now its clear to me how it works
 
Chitra AP
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swetha,

I didn't get it correctly. Could you please explain? Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic