Sling Academy
Home/Kotlin/Checking Equality and Inequality of Values in Kotlin

Checking Equality and Inequality of Values in Kotlin

Last updated: December 05, 2024

Kotlin, a modern programming language that targets the JVM, Android, JavaScript, and Native, provides various ways to compare values for equality and inequality. Understanding how to efficiently compare values is crucial for writing conditional logic, debugging, and preventing unintended behavior in your applications.

Basic Equality and Inequality Operators

Kotlin provides simple operators == and != to compare two values for equality and inequality respectively.

Using ==

In Kotlin, == is used to compare two values to check if they are equal. This operator is safe and performs nullable checks. This means it checks for structural equality rather than referential equality.


val a = 5
val b = 5
println(a == b)  // Output: true

Using !=

The != operator is used to check if two values are not equal:


val a = 5
val b = 10
println(a != b)  // Output: true

Difference between == and equals()

The == operator in Kotlin is actually a shorthand for equals(). When you use ==, it translates internally to a call to equals() method on the left object.


data class Person(val name: String)

val person1 = Person("John")
val person2 = Person("John")
println(person1 == person2)      // Output: true
println(person1.equals(person2)) // Output: true

In the context of data classes, equals() is automatically overridden to compare properties, making == very effective for comparing objects of data classes.

Reference Equality with ===

To check if two references point to the same object, Kotlin provides the === operator:


val x = listOf("a", "b", "c")
val y = listOf("a", "b", "c")
println(x === y)  // Output: false
println(x == y)   // Output: true

In the example above, === evaluates to false because x and y are different objects in memory (although they contain equal elements). However, == evaluates to true because it compares the structure.

Handling Nullable Types

Kotlin's null safety features become evident when comparing nullable types. The == operator is designed to handle nulls proficiently without throwing null pointer exceptions.


var nullableValue: String? = null
println(nullableValue == null)  // Output: true

nullableValue = "Hello"
println(nullableValue == "Hello")  // Output: true

As demonstrated, Kotlin null-safely checks values on both sides when evaluating equality.

Comparing with compareTo()

For comparing values where order matters (like numbers or strings), you can use the compareTo() method:


val value1 = 42
val value2 = 50
println(value1.compareTo(value2)) // Output: -1 (value1 less than value2)

val string1 = "kotlin"
val string2 = "java"
println(string1.compareTo(string2)) // Output: 7 (string1 greater than string2)

The compareTo() method returns an integer value; 0 indicates equality, a negative number indicates the first value is less than the second, and a positive number indicates the first value is greater than the second.

Conclusion

In summary, Kotlin offers a versatile set of tools for checking equality and inequality, accommodating both structural and referential comparisons. By leveraging operators and functions like ==, !=, ===, compareTo(), and understanding their behavior with nulls and objects, you can implement robust comparison logic in your Kotlin applications.

Next Article: Using Boolean Expressions in If Statements in Kotlin

Previous Article: Understanding Conditional Expressions in Kotlin

Series: Primitive data types in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin