Checking for equality in Kotlin

For prospective students on the course “Kotlin Backend Developer” prepared a translation of useful material.

We also invite you to watch an open lesson on the topic “Kotlin multiplatform: Front / Back in one language”.


There are three ways to check for equality in Kotlin language:

The first way is to compare structures (==)

Operator == in Kotlin allows you to compare data contained in variables. However, in Java, this operator is used to compare references of two variables.

In case of custom classes == can be used to compare the contents of data classes. Otherwise, this operator compares references.

The second way is comparing links (===)

Operator === used in Kotlin to compare references of two variables. However, in the case of primitives === is equivalent ==, that is, it checks the values.

The third way is the method equals

Method equals performs the same function in Kotlin as ==

However, between the method equals and operator == there is a difference in the case of comparing variables of type Float and Double… If, when comparing variables of type Float and Double through == the standard applies IEEE 754, then in the case of equals it is not, so when using equals It is believed that:

  • NaN is equal to itself;

  • NaN is greater than any other element, including POSITIVE_INFINITY;

  • 0.0 is less than 0.0.

Let’s look at the examples below to better understand how the comparison works in different cases.

1. Comparison of primitives

val firstInt = 5
val secondInt = 5

println(firstInt == secondInt)  // true
println(firstInt === secondInt) // true
println(firstInt.equals(secondInt)) // true

For primitive data types, the values ​​they contain are compared.

2. Comparison of wrapped primitives

val firstInt = Integer(5)
val secondInt = Integer(5)
println(firstInt == secondInt)  // true
println(firstInt === secondInt) // false
println(firstInt.equals(secondInt)) // true

Here firstInt and secondInt have different links. Therefore, comparison of links (===) returns the result false… When checking for structure equality and using the method equals only the content is checked. Therefore, the result of the check will be truesince the value of both variables is 5

3. Comparison of objects of custom classes

class Student(val name : String)

val student1 = Student(“Jasmeet”)
val student2 = Student(“Jasmeet”)

println(student1 === student2) // false
println(student1 == student2) // false
println(student1.equals(student2)) // false
println(student1.name === student2.name) // true
println(student1.name == student2.name) // true
println(student1.name.equals(student2.name)) // true

In this case student is neither a primitive nor a wrapper, so in all cases references are compared, not content. However, when comparing string literals, the content is compared in the same way as in Java.

For content comparison to work, you need to deal with a data class.

data class Student(val name : String)

val student1 = Student(“Jasmeet”)
val student2 = Student(“Jasmeet”)

println(student1 === student2) // false
println(student1 == student2) // true
println(student1.equals(student2)) // true

4. Comparison of negative and positive zero

val negativeZero = -0.0f
val positiveZero = 0.0f

println(negativeZero == positiveZero) // true
println(negativeZero.equals(positiveZero)) // false

As mentioned, when comparing negative zero and positive zero using the operator == the IEEE 754 standard applies. This returns the value true… When using the same method equals this standard does not apply, so a value is returned false

Used materials: https://kotlinlang.org/docs/reference/equality.html


Learn more about the course “Kotlin Backend Developer”.

View open lesson on the topic “Kotlin multiplatform: Front / Back in one language”.


GET A DISCOUNT

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *