2

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!

4
  • what is null here ? variable or null try to do trim() operation. – Ankuli Aug 7 '15 at 12:47
  • It's not a variable. Just a null. – Lalitha A Aug 7 '15 at 12:51
  • this can be also used str.replace('null','') – Ankuli Aug 7 '15 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 Aug 7 '15 at 13:01
3

Interesting ..

I tried this in dev console

string sone = null + ',' + null;
system.debug(sone);
system.debug(sone.length());

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;
system.debug(stwo);
system.debug(stwo.length());

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

1

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:

Also,

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.

1
  • 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 Aug 7 '15 at 19:52
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.