

> a1 = a2 // a1 and a2 have references to different objectsįor regular classes, the implementation of equals is inherited from Any, and just make the object equal to itself. > a1 = a2 // a1 and a2 are different instances of A Let's assume the definition of A is as you have defined in your question. For values which are represented as primitive types at runtime (for example, Int ), the = equality check is equivalent to the = check. Referential Equality ( =)Ī = b evaluates to true if and only if a and b point to the same object. Functions with the same name and other signatures, like equals(other: Foo), don't affect equality checks with the operators = and !=. To provide a custom equals check implementation, override the equals(other: Any?): Boolean function. If a is not null, it calls the equals(Any?) function, otherwise it checks that b is referentially equal to null. Referential equality (two references point to the same object) => =ĭetailed answer: Structural Equality ( =)īy convention, an expression like a = b is translated to: a?.equals(b) ?: (b = null).Structural equality (a check for equals()) => =.In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that cannot (non-null references).In Kotlin there are two types of equality: Other issues caused by external Java code. For example, a piece of Java code might add null into a Kotlin MutableList, therefore requiring a MutableList for working with it. Nullability issues with generic types being used for Java interoperation. Usage of the !! operator that is described below.ĭata inconsistency with regard to initialization, such as when:Īn uninitialized this available in a constructor is passed and used somewhere (a "leaking this").Ī superclass constructor calls an open member whose implementation in the derived class uses an uninitialized state.Īttempts to access a member of a null reference of a platform type The only possible causes of an NPE in Kotlin are:Īn explicit call to throw NullPointerException(). In Java this would be the equivalent of a NullPointerException, or an NPE for short. One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception.

Kotlin's type system is aimed at eliminating the danger of null references, also known as The Billion Dollar Mistake. Null safety Nullable types and non-null types
