Why is =+ (equals plus) valid in Apex?

394    Asked by ranjan_6399 in Salesforce , Asked on Apr 24, 2021

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 throw an #error!

That is exactly what it is:

Integer i; i += 1;

This gives dereference null object #error!

i =+ 1;

i is now equal to one.

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



Your Answer

Interviews

Parent Categories