• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Doubt.......null?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
operator overloading. string concatenation.
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement



is equivalent to



StringBuffer's append() method can handle null args.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all of you for clearing the doubt and providing such a lucid explanation .

Bye.
 
Philip Heller
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, Rakesh is right. I meant to say
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your efforts and time.Now its clear to me how it works
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Swetha,

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