I was trying to do some operations on a delimited string. I chanced upon a case where I got to check the length of a delimited string with nulls in it.

For Example :

String str = null+', '+null

Now str.length() gives me 10.

When I change str to ''+', '+'' , the length method returns 2.

I was just wondering, if null has no value in it, why does the string take it into account while calculating the length of it ?

Thanks in advance!

  • what is null here ? variable or null try to do trim() operation.
    – Ankuli
    Commented Aug 7, 2015 at 12:47
  • It's not a variable. Just a null.
    – Lalitha A
    Commented Aug 7, 2015 at 12:51
  • this can be also used str.replace('null','')
    – Ankuli
    Commented Aug 7, 2015 at 12:54
  • This replaces all nulls in the string. But if there is an actual string called null in it, for ex: str = 'null'+', '+null ( one null is a string, other is just a null ), It replaces both of them which is not the purpose.
    – Lalitha A
    Commented Aug 7, 2015 at 13:01

3 Answers 3


Interesting ..

I tried this in dev console

string sone = null + ',' + null;

and the result is

23:21:38.057 (57852690)|USER_DEBUG|[2]|DEBUG|null,null
23:21:38.057 (57922386)|USER_DEBUG|[3]|DEBUG|9

and then tried this

string stwo = null;

result is

23:24:01.057 (57435220)|USER_DEBUG|[5]|DEBUG|null
23:24:01.057 (57698518)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

I m no Java expert but since Apex is based on Java, did some searching on null in java and found this discussion

concatenating null strings in java

which says

If the reference is null, it is converted to the string "null"

so your initial string concatenation of 2 nulls and comma and a space adds up to 10 in length.

another useful discussion around null in java for additional reference.

What is NULL in Java


You should learn about Operator Overloading or operator ad hoc polymorphism.

So when you do String str = null+', '+null it result into a text:

null, null // String concatenation takes place by + operator

Try this:

String nops = null+null // Arithmetic calculation takes pace by '+'

You will get an exception:


when you do String str = ''+', '+'' it result into a text having two Characters space and comma:

, // String concatenation takes place by + operator and there are two character.

> Line: x, Column: y
>     Arithmetic expressions must use numeric argument

So, the + operator behaves according to the data type you are joining with it. Technically it is operator overloading.

  • note that SFDC formula fields that use & operator against an sobject field value that is null will concatenate empty string, not 'null' (4 char string).
    – cropredy
    Commented Aug 7, 2015 at 19:52

The reason String str = null+', '+null has a length of 10 is that each null is taken as a literal text value (4 characters each) plus ', ' is an addition 2 characters for a total of 10 characters.

In the situation with ''+', '+'' only the ', ' is counted for a total of 2 characters.

With strings, the concept of null has little meaning. That's why strings should be checked using isEmpty() instead of tested for equality with null.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .