? in Salesforce Apex used for Safe Navigation Operator.
The safe navigation operator (?.) can be used for checking null references.
Sample Code:
Account objAcc;
if ( String.isNotBlank( objAcc.Name ) ) {
System.debug(
'Account name is ' +
objAcc.Name
);
}
“Attempt to de-reference a null object” Exception will be thrown for the above code since objAcc is null.
So, the safe navigation operator (?.) can be used in this case to avoid the null pointer exception.
Sample Code with Safe Navigation operator (?.):
Account objAcc;
if ( String.isNotBlank( objAcc?.Name ) ) {
System.debug(
'Account name is ' +
objAcc.Name
);
}