So many times, we do a null check of a field value and then compare them

boolean checkAmountGreaterThan10k(Opportunity record){
       return record.Amount > 10000;
    return false;

Then I recently discovered that any comparison operation performed on null value returns false.

Something like :

Integer bla = null;
System.debug(bla > 10000);//Prints 'false'
System.debug(bla >= 10000);//Prints 'false'
System.debug(bla < 10000);//Prints 'false'
System.debug(bla <= 10000);//Prints 'false'

System.debug( bla + 10000); // throws Null Ptr Exception

Thus I was thinking to use this in the above checkAmountGreaterThan10k as

boolean checkAmountGreaterThan10k(Opportunity record){  
       return record.Amount > 10000;    

Is this a documented behavior of comparison operator involving null, I was thinking about saving many lines of code with this.

I came from java world where I used to check null every time, similar java equivalent code throws NullPtrException

class Ideone
    public static void main (String[] args) throws java.lang.Exception
        System.out.println("Hello World");
        Integer i = null ;
        System.out.println(i < 20); //Exception in thread "main" java.lang.NullPointerException,

Java code link:

Is the Apex Runtime not throwing a null ptr exception in Apex a bug or undocumented feature?

  • 1
    Numeric comparison against false used to throw NPE, I am 100% certain. Interesting that they would make this change and not announce it.
    – Adrian Larson
    Commented Oct 24, 2019 at 18:48

2 Answers 2


I find this behavior documented in the Apex Developer Guide's Expression Operators for the comparison operators:

  • Less than operator
  • Greater than operator
  • Less than or equal to operator
  • Greater than or equal to operator.

If x or y equal null and are Integers, Doubles, Dates, or Datetimes, the expression is false.

  • 1
    Ah nice... awesome that I can use it without worrying Commented Oct 24, 2019 at 18:59

Surprisingly (to me), this behavior is documented. I have pasted the reference for <, but the same statement is made for <=, >, and >=.

Operator <

x < y

Less than operator. If x is less than y, the expression evaluates to true. Otherwise, the expression evaluates to false.

  • Unlike other database stored procedures, Apex does not support tri-state Boolean logic, and the comparison of any two values can never result in null.
  • If x or y equal null and are Integers, Doubles, Dates, or Datetimes, the expression is false.
  • A non-null String or ID value is always greater than a null value.
  • If x and y are IDs, they must reference the same type of object. Otherwise, a runtime error results.
  • If x or y is an ID and the other value is a String, the String value is validated and treated as an ID.
  • x and y cannot be Booleans.
  • The comparison of two strings is performed according to the locale of the context user and is case-insensitive.

On the contrary, the following operators note

x and y cannot be null

  • +=
  • *=
  • -=
  • /=
  • |=
  • &=
  • &&
  • ||

Surprisingly, this indication is not made for arithmetic operations, even though it still holds.

You must log in to answer this question.

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