Kotlin Relational (AKA Comparison) Operators Tutorial

In this section, we will learn what the Relational operators are and how to use them in Kotlin.

What are the Relational Operators in Kotlin? (Comparison Operators)

The relational (AKA comparison) operators are used to compare two values and see which one is greater than the other or whether they are equal.

List of Relational Operators in Kotlin

In the table below, you can see the list of these operators.

Operator Symbol
Equal ==
Not equal !=
Greater than >
Less than <
Greater Than or equal to >=
Less than or equal to <=

Conditional Statements

Before we get into the details of comparison operators and see them each, you should know that the main use case of these operators is when we have a conditional statement like if statement or while loop etc. Basically, these conditional statements define a set of instructions that will only run if a condition is met! Like for example, if the value of a variable was higher than x or if that value was equal to another value, etc.

So the use of comparison operators is mainly within the conditions of these types of statements.

For this reason, we will start with one of these conditional statements (the if statement to be exact) and after that, we continue our discussion on comparison operators.

Conditional Statements: If statement

The if statement is one of those conditional statements that defines a body where you can put a set of instructions. The If statement also has a condition part which will be evaluated first and if the result of that condition was true, then the instructions within its body will be executed! Otherwise, if the result of the condition becomes false, the body of the if statement will be ignored.

Here’s the syntax of the if statement:

if (condition) {

#instructions to be executed if the condition of the if statement resulted true.

}

if: in order to create an if statement, we first start with the keyword if.

(condition): after the if statement we put the condition to be evaluated.

{}: the open and close curly braces define the border of the if statement’s body where we can set the needed instructions that will only run if the condition of the if statement evaluated to true.

Note: the if statement has more syntax and details but for this section, what you’ve learned will be enough. In the Kotlin if statement section we’ve explained this conditional statement in greater details.

If statement Example:

fun main(){
    var age  = 60 

    if (age >50 ){
        println("The value of the age is above 50")
    }
}

Output:

The value of the age is above 50

In this example, if we look at the condition of the if statement, we can see that there’s a comparison between the current value of the age variable and the value 50 using the Greater Than operator, which is a conditional operator! Here, we’ve checked to see if the age value is greater than the value 50 and because it is, then the result of the comparison will be true (hence the result of the condition is true) and so the body of the if statement is allowed to be executed.

That’s how we got the message you see on the output.

Alright, now that we know how the if statement works, let’s get into the details of comparison operators and see how they work.

Kotlin Equal == Operator:

The equal operator is used to see if two values are equal in value or not. If they were, the return value of this operator will be the boolean value true, otherwise false.

Example: using equal operator in Kotlin

fun main(){
    var height = 100 

    if (height == 100){
        println("The height is equal to 100")
    }
    println("End")
}

Output:

The height is equal to 100

End

Here the condition of the if statement is to see if the value of the height variable is equal to the value 100 using the == operator.

Now, because this comparison is true, then the condition is true and so the body of the if statement ran as a result.

Kotlin Not Equal != Operator:

The not equal operator is on the opposite side of the equal operator and is used when we want to see if two values are not equal!

If the two involved operands were not equal, then the result of this operator will be true. Otherwise, the value false will return instead.

Example: using not equal operator in Kotlin

fun main(){
    var height = 100 

    if (height != 96){
        println("The height is not equal to 96! It's actually ${height}")
    }
    println("End")
}

Output:

The height is not equal to 96! It’s actually 100

End

In this example, we know that the value of the height variable is 100. And so when we’ve compared the value of the variable with the value 100 to see if they are not equal, the result of the comparison is true and so the body of the if statement ran as a result.

Kotlin Referential Equality (AKA Identity Equality) === Operator

This operator checks the identity of the two operands! Basically, it checks to see if the two operands (the two values) are the same object! Specifically, it checks to see if the two values are coming from the same memory location! In that case only, the result of the comparison will be true. Otherwise, the value false will return instead.

Note that the == operator checks the two operands to see if they are equal in value! For example, if there are two number values and both are 10 but each is stored in a different memory location, then using the equal operator == will return the value true while the identity equality === operator returns false.

Example: using referential equality operator in Kotlin

fun main(){
    var height = Integer(100)
    var height2 = Integer(100)

    if (height === height2){
        println("The two variables are referencing to the same object.")
    }else{
        println("The two variables are pointing to different objects so they don't have the same identity")
    }
    println("End")
}

Output:

The two variables are pointing to different objects so they don't have the same identity

End

Note: we didn’t mention the else statement yet but in short it’s part of the if statement and we can put instructions in its body that will run only if the condition of the if statement resulted false! Think about it as the plan B of the if statement in your program!

In this example, we have two objects named height and height2. These objects have the same value but they are two separate entities (Which means they’re stored in two separate memory locations). For this reason, when we’ve compared them using the identity equality operator === the result of the comparison was false and so the body of the if statement is skipped and the body of the else statement ran instead.

Kotlin Greater Than > Operator:

The Greater Than operator is used to seeing if the operand on the left side of this operator is greater than the right side operand.

If it was, then the result of this comparison will be true. Otherwise, the value false will return instead.

Example: using greater than operator in Kotlin

fun main(){
    var age = 50 

    if (age > 40){
        println("The value of the age variable is greater than 40")
    }else{
        println("The value of the age variable is less than 40")
    }
    println("End")
}

Output:

The value of the age variable is greater than 40

End

Here, the value of the age variable is greater than 40 and so the result of the comparison in the condition of the if statement resulted true and the body of this statement ran as a result.

Kotlin Less Than < Operator:

The less than operator is on the opposite of the greater than operator and it is used to see if the left side operand is less than the right side operand.

If it was, then the result of this operator will be true. Otherwise, the value false will return instead.

Example: using less than operator in Kotlin

fun main(){
    var age = 50 

    if (age < 40){
        println("The value of the age variable is less than 40")
    }else{
        println("The value of the age variable is greater than 40")
    }
    println("End")
}

Output:

The value of the age variable is greater than 40

End

Here the condition of the if statement is to see if the age variable is less than the value 40. But we know that this comparison is not true and so the if statement is skipped and the body of the else statement ran as a result.

Kotlin Greater than or Equal To >= Operator:

The Greater than or Equal operator is used to check the two involved operands for two conditions:

  • The operator checks to see if the left operand is greater than the right side operand.
  • The operator checks to see if both involved operands are equal in value.

Now, if either of these comparisons resulted true, then the final result of the operator will be true as well. Otherwise, the value false will return instead.

Example: using greater than or equal operator in Kotlin

fun main(){
    var age = 50 

    if (age >= 40){
        println("The value of the age variable is greater or equal to the value 40")
    }else{
        println("The value of the age variable is less than 40")
    }
    println("End")
}

Output:

The value of the age variable is greater or equal to the value 40

End

Kotlin Less Than or Equal To <= Operator:

The less than or equal operator is used to check the two involved operands for two conditions:

  • The operator checks to see if the left operand is less than the right side operand.
  • The operator also checks to see if both involved operands are equal in value.

Now, if either of these comparisons resulted true, then the final result of the operator will be true as well. Otherwise, the value false will return instead.

Example: using less than or equal operator in Kotlin

fun main(){
    var age = 50 

    if (age <= 40){
        println("The value of the age variable is less or equal to the value 40")
    }else{
        println("The value of the age variable is greater than 40")
    }
    println("End")
}

Output:

The value of the age variable is greater than 40

End
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies