No NPE when calling '==' on null


#1

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?


#2

== is defined on Any, per https://www.scala-lang.org/files/archive/spec/2.12/12-the-scala-standard-library.html

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.

-dave-