I often see in source codes the usage of if (object.ReferenceEquals(myObject, null)) for checking if myObject is null instead of if (myObject == null) which I am familiar with.
Is there any particular reason (like speed, readability, etc) for using the first way instead of the second one? Which one do you use?
Thank you in advance.
Typically, you'll check for null using the triple equality operator ( === or !== ), also known as the strict equality operator, to be sure that the value in question is definitely not null: object !== null . That code checks that the variable object does not have the value null .
In C or C++, there is no special method for comparing NULL values. We can use if statements to check whether a variable is null or not.
In order to check whether a Java object is Null or not, we can either use the isNull() method of the Objects class or comparison operator.
Use the ISNULL function with the IF statement when you want to test whether the value of a variable is the null value. This is the only way to test for the null value since null cannot be equal to any value, including itself. The syntax is: IF ISNULL ( expression ) ...
Simple things are usually the most efficient :
(myObject == null)
is more performant
Look at this article
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With