No NPE when calling '==' on null


val str1 = “Scala”
val str2 = null
str2 == str1

This gives false.
I understand this causes AnyRef’s == being called.
But as str2 is null, why I don’t get NullPointerException?


== is defined on Any, per

Any's implementation looks like this:

/** Semantic equality between values */ final def == (that: Any): Boolean = if (null eq this) null eq that else this equals that }

package scala /** The universal root class */ abstract class Any {

No dereferencing of a null reference happens, so no NPE.