Relational operators are fundamental building blocks in programming languages that allow developers to compare values and make decisions based on those comparisons. In Kotlin, a modern and concise programming language developed by JetBrains, relational operators are essentials in controlling the flow of a program. In this article, we’ll explore Kotlin relational operators, provide detailed explanations, and code examples to help you grasp their usage.
What Are Relational Operators?
Relational operators, also known as comparison operators, are symbols used to compare two values. Kotlin provides a set of relational operators that allow you to express relationships between variables and make decisions based on these relationships. The result of a relational operation is always a Boolean value—either true or false.
Here are the primary relational operators in Kotlin:
Equality Operator (==)
The equality operator (==) is used to check if two values are equal. It is an essential operator in any programming language, and Kotlin is no exception. Consider the following example:
fun main() {
val a = 5
val b = 5
val result = (a == b)
println("Are a and b equal? $result")
}
In this example, the variables a and b are both assigned the value 5. The equality operator is then used to compare these values, and the result is true since a is indeed equal to b.
Inequality Operator (!=)
The inequality operator (!=) checks if two values are not equal. Let’s modify the previous example to use the inequality operator:
fun main() {
val a = 5
val b = 7
val result = (a != b)
println("Are a and b not equal? $result")
}
Now, a is not equal to b, so the result is true.
Greater Than Operator (>)
The greater than operator (>) compares two values and returns true if the left operand is greater than the right operand; otherwise, it returns false.
fun main() {
val p = 10
val q = 5
val isGreaterThan = (p > q)
println("Is p greater than q? $isGreaterThan")
}
Here, the program evaluates whether p is greater than q and prints the result.
Less Than Operator (<)
Similarly, the less than operator (<) determines if the left operand is less than the right operand. It returns true if the condition is met and false otherwise.
fun main() {
val m = 3
val n = 8
val isLessThan = (m < n)
println("Is m less than n? $isLessThan")
}
In this example, the program compares the values of m and n using the less than operator. The isLessThan variable will be true because m is less than n.
Greater Than or Equal To Operator (>=)
The greater than or equal to operator (>=) checks if the left operand is greater than or equal to the right operand. It returns true if the condition is satisfied and false otherwise.
fun main() {
val num1 = 15
val num2 = 15
val isGreaterOrEqual = (num1 >= num2)
println("Is num1 greater than or equal to num2? $isGreaterOrEqual")
}
Here, the program evaluates whether num1 is greater than or equal to num2 and prints the result accordingly.
Less Than or Equal To Operator (<=)
Similar to the greater than or equal to operator, this checks if the value on the left is less than or equal to the value on the right.
fun main() {
val price1 = 50
val price2 = 75
val isLessOrEqual = (price1 <= price2)
println("Is price1 less than or equal to price2? $isLessOrEqual")
}
In this instance, the program compares the values of price1 and price2 using the less than or equal to operator. The isLessOrEqual variable will be true since price1 is less than price2.
Referential Equality
While relational operators in Kotlin, such as == and !=, focus on comparing the content or values of objects, referential equality introduces a distinct perspective. The === operator, also known as the referential equality operator, is designed to evaluate whether two references point to the exact same object in memory. This adds another layer to your understanding of equality in Kotlin and is particularly relevant when dealing with object instances.
In Kotlin, every object is allocated a unique memory address, and referential equality aims to determine if two references point to the identical memory location. This goes beyond checking whether the contents of two objects are the same; it examines whether they are, in fact, the very same object. Consider the following example:
fun main() {
val list1 = listOf(1, 2, 3)
val list2 = listOf(1, 2, 3)
val list3 = list1
println("list1 == list2: ${list1 == list2}")
println("list1 === list2: ${list1 === list2}")
println("list1 === list3: ${list1 === list3}")
}
In this example, list1 and list2 have the same content, so the result of == is true. However, they are distinct objects with different memory addresses, leading to false when using ===. On the other hand, list3 references the same object as list1, resulting in true for ===.
Nullable Types and Safe Calls
Kotlin’s type system includes nullable types, and when working with nullable variables, it’s crucial to use the safe call operator (?.) to avoid null pointer exceptions.
fun main() {
val nullableValue: Int? = null
val result = nullableValue?.equals(42)
println("Is the nullable value equal to 42? $result")
}
In this example, if nullableValue is null, the result will be null, and no exception will be thrown.
String Comparison
Relational operators are not limited to numerical values; they can also be used with strings for lexicographical comparison. Consider the following example:
fun main() {
val name1 = "Cherish"
val name2 = "Lucy"
val result = (name1 < name2)
println("Is str1 less than str2? $result")
}
In this case, the strings “Cherish” and “Lucy” are compared based on their lexicographical order, and the result is true.
Conclusion
Relational operators are powerful tools in Kotlin that enable developers to express relationships between values and make decisions in their programs. Whether you’re comparing numerical values, or strings, understanding how to use these operators is essential for writing efficient and expressive code.
In this article, we explored the equality and inequality operators, as well as the greater than, less than, greater than or equal to, and less than or equal to operators. We also saw how these operators can be applied to different data types, including strings.
Sources: