Equality in Kotlin (‘==’, ‘===’ And ‘EQUALS’)

Since Kotlin was created using the JVM, it is equivalent to Java and statistically typed. There are two kinds of equality tests in Kotlin; one is indicated by “==” and the other by “===” (Kotlin triple equals).

A further feature Kotlin offers is the ability to compare instances of a given type in two ways. Kotlin distinguishes itself from other programming languages because of this characteristic. There are two sorts of equality: Referential equality and structural equality.

In Kotlin, we frequently need to compare the information from two variables, objects, or references from two objects. This raises the issue of which equality check to employ in particular circumstances. Let’s examine the different checks that Kotlin offers.

Kotlin Triple Equals: Overview

Check this out: Kotlin Array Length: Everything To Know

Structural Equality (‘==’)

Two variables’ data are compared using the structural equality (‘==’) “==” operator.

structural equality

Do not confuse this equality operator with the Java == operator since they are two distinct operators. While in Java or other languages, the == operator is typically used to compare references, the == operator in Kotlin simply compares data or variables. When two values are not equal, the negated version of the == operator in Kotlin is used to compare.

Referential equality (‘===’)

The reference of two variables or objects is compared using the “===” operator. It is true solely when both variables or entities point to the same item. In Kotlin,!== is used to compare if two values are not equal. It is the negated equivalent of ===. The equality check of === is comparable to that of == for runtime values represented as primitive types (for instance, Int).

.equals method

Every class implements the equals method, which any extending class may override. Similar to the == operator, the equals method compares the contents of variables or objects. However, it works differently when comparing Float and Double.

The distinction between == and equals is present when comparing Float and Double values. The IEEE 754 Floating-Point Arithmetic Standard clashes with equals.

Kotlin “==” vs “===”

According to the official literature, “==” and “===” (kotlin triple equals) are used for structural and referential equality, respectively.

For whichever expression,

Only when both “a” and “b” have equal values will the expression “a==b” be evaluated as True.

Only when “a” and “b” are pointing at the same object can the expression “a====b” be evaluated as True.

Kotlin example:

In this example, we’ll show how these two operators—” ==” and “===”—achieve their desired results.


fun main(args: Array<String>) {

   val student1 = "Ram"

   val student2 = "shyam"

   val student4 = "Ram"

   val student3=student1

   // prints true as both pointing to the same object

   println(student1 === student3)

   // prints false

   println(student1 === student2)

   //prints true

   println(student1 == student4)

}

Output:

Upon run, it will result in the output seen below:

true

false

true

See also: Kotlin Tutorial: Add New Element To Array

Kotlin “==” vs Kotlin .equal method

Only a data class qualifies for the content comparison to work. Provided it’s a conventional class, the compiler regards both objects as separate even though what they contain is similar; however, if it’s a data class, the compiler examines the data and then gives true if the information inside is identical.

Change the class mentioned above to a data class.


data class Employee (val name: String)

val emp1 = Employee("Suneet")

val emp2 = Employee("Suneet")

println(emp1 == emp2) //true

println(emp1.equals(emp2)) //true

println(emp1 === emp2) //false

println(emp1.name == emp2.name) //true

println(emp1.name.equals(emp2.name)) //true

println(emp1.name === emp2.name) //true

Finally, let's contrast the float readings with a negative and a positive zero.

val negZero = -0.0f

val posZero = 0.0f

println(negZero == posZero) //true

println(negZero.equals(posZero)) //false

println(negZero === posZero) //true

The IEEE 754 Standard for Floating-Point Arithmetic and. equals are in conflict because.equals delivers a false result when -0.0 is compared to 0.0, whereas == and === (kotlin triple equals) do the opposite.

Example

Comparing two variables of the basic type Int.


val int1 = 10

val int2 = 10

println(int1 == int2) //true

println(int1.equals(int2) //true

println(int1 === int2) //true

Rather than a primitive datatype, use a wrapper class

val first = Integer(10)

val second = Integer(10)

println(first == second) //true

println(first.equals(second)) //true

println(first === second) //false

Class object

class Employee(val name: String)

val emp1 = Employee("Dmitry")

val emp2 = Employee("Dmitry")

println(emp1 == emp2) //false

println(emp1.equals(emp2)) //false

println(emp1 === emp2) //false

println(emp1.name == emp2.name) //true

println(emp1.name.equals(emp2.name)) //true

println(emp1.name === emp2.name) //true

The comparison above was made because Empoyee is neither a primitive datatype nor a wrapper class. As a consequence, all three tests produced incorrect findings. However, when comparing strings, it just verifies that the contents of the string were equal, returning true in every instance.

FAQs

Can Kotlin compare custom objects with '=='?

However, it also relies on how the custom class implements the equals function. You can design your equality logic by overriding equals.

What distinguishes '==' from 'equals' for custom objects?

The comparison operator '==' evaluates referential equality (if two references link to the same object). To compare structural equality (if the substance of two objects is the same), 'equals' should be overridden in custom classes.

How can I make Kotlin's equality comparison specific to my classes?

Override the equals method in your class to carry out your equality logic. To preserve consistency, you should additionally alter the hashCode function.

When is '===' preferable to '=='?

When determining if two references are pointing at the same memory item, use the operator '===.' This helps determine whether two variables reference the same instance exactly.

Can I use Kotlin's '= =' operator with nullable types?

'==' can be used with nullable types. Even if both values are nil, it will still determine if they are equal.

What happens if I don't make 'equals' in a custom class overridable?

'equals' will act like '==' and perform a referential equality check if you don't override it.

What makes the Kotlin string operators' ==' and '===' different?

In the case of strings, '==' determines if the strings' contents are identical, whereas '===' determines whether they refer to the same string instance.

Is there a distinction between '==' and 'equals' in Kotlin when comparing lists or arrays?

Yes, '==' compares the contents of lists or arrays, but 'equals' does not immediately apply to lists or arrays unless you implement it in your class.

Conclusion

This article demonstrated the distinction between referential and structural equality in Kotlin with a straightforward example. As always, GitHub is where you can get the finished product of all of these instances and snippets.

Because this project is Maven-based, importing and running without modification should be simple.

Since Kotlin lacks a constructor that takes the form String(“”), all string comparisons will return true if the contents are the same.

When explicitly comparing to null, there is no purpose in code optimization. Because null is a reference and comes at the end, a == null will immediately transform to a === (kotlin triple equals) null.

See also: Kotlin Parameterized Test: What Is It?

Leave a Comment