Just fixed an unfortunate bug. I was trying to concatenate a string. But it kept getting replaced by the string it should have been adding. Couldn't figure it out for a while.

I was doing the operation like this:

attendeesListFormatted =+ attendeeFormated;

Took me a while to see my dyslexic mistake. Then I wondered why is that valid? Wouldn't it encounter an unexpected character after the assignment?

Then it occurred to me that maybe Apex was treating it as a null concatenation. Like so:

attendeesListFormatted = '' + attendeeFormated;

Can anyone think of another reason my mistake wouldn't thrown an error?


That is exactly what it is:

Integer i;
i += 1;

This give dereference null object error

i =+ 1;

I is now equal to one.

I cannot find this documented in the Salesforce Operators documentation though.

  • 7
    It makes sense if you read it like i = +1 – rael_kid Oct 24 '14 at 18:38

As Lex commented, what you have with i =+ 1 is the Assignment operator (Right associative) and the positive equivalent of the Unary negation operator : the Unary Plus Operator.

It does seem odd that you can use a unary operator on a string, were the + is otherwise used for concatenation with two operands. Typically the Unary Plus Operator requires a numeric operand. In this case I'd expect an Integer or Double.

See Understanding Expression Operators

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.