Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringBuilder appends null as "null"

How come?

    StringBuilder sb = new StringBuilder();

    sb.append("Hello");
    sb.append((String)null);
    sb.append(" World!");
    Log.d("Test", sb.toString());

Produces

06-25 18:24:24.354: D/Test(20740): Hellonull World!

I expect that appending a null String will not append at all!!
BTW, the cast to String is necessary because StringBuilder.append() has a lot of overloaded versions.

I simplified my code to make a point here, in my code a got a method returning a string

public String getString() { ... }
...
sb.append(getString());
...

getString() sometimes returns null; now I have to test it if I want to use a StringBuilder!!!!

Furthermore, let's clarify my question here: how can I get StringBuilder.append() to accept, in an elegant way, a null string, and not convert it to the literal string "null".
By elegant I mean I don't want to get the string, test if it is null and only then append it. I am looking for an elegant oneliner.

like image 487
ilomambo Avatar asked Jun 26 '13 08:06

ilomambo


4 Answers

How come?

Its clearly written in docs

The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument. If str is null, then the four characters "null" are appended.

So please put a null check before append.

like image 156
Suresh Atta Avatar answered Oct 12 '22 21:10

Suresh Atta


That's just the way it is. null is appended as "null". Similarly, when you print null with System.out.println(null) you print "null".

The way to avoid that is that method that returns string getString() checks for null:

public String getString() {
    ...
    if (result == null) {
        return "";
    }
}
like image 31
darijan Avatar answered Oct 12 '22 19:10

darijan


It behaves like this, because null isn't an empty String as you think of it. Empty Stringwould be new String("");. Basically append() method appends "null" if it gets null value as a parameter.

like image 44
Piotr Chojnacki Avatar answered Oct 12 '22 21:10

Piotr Chojnacki


EDIT
My previous answer was not reflecting the appropriate reason for such behaviour of StringBuilder as asked by OP. Thanks to @Stephen C for pointing out that

I expect that appending a null String will not append at all!!

But it is appending. The reason for such behaviour of StringBuilder is hidden in implementation detail of append(Object obj) method which takes us to AbstractStringBuilder#append(String str) method which is defined as follows:

public AbstractStringBuilder append(String str) {
    if (str == null) str = "null"; -->(1)
    //.......
    return this;
}

It clearly specifying that if the input String is null then it is changed to String literal "null" and then it processes further.

How can I get StringBuilder.append() to accept, in an elegant way, a null string, and not convert it to the literal string "null".

You need to check the given String for null explicitly and then proceed. For example:

String s = getString();
StringBuffer buf = new StringBuffer();
//Suppose you want "Hello "<value of s>" world"
buf.append("Hello ");
buf.append(s==null?" world" : s+" world");
like image 41
Vishal K Avatar answered Oct 12 '22 20:10

Vishal K