Equality and Relational Operators in Kotlin

Start course
Overview
Difficulty
Intermediate
Duration
46m
Students
10
Ratings
5/5
starstarstarstarstar
Description

This course takes a deep dive into Kotlin operators, giving you a practical understanding of how to use operators in your code.

Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.

Prerequisites

This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.

Transcript

Alright, hello, my friends. So, in this lesson we're going to talk about the equality and relational operators. So, the equality and relational operators determine if one operand is greater than, less than, equal to or not equal to another operand, alright? So, let's have a look at how they work. Equal equal (==); so, this operator checks to see if the values of two operands are equal or not. If yes, then the condition becomes true. Here is the exclamation mark and equal (!=). So, this operator checks to see if the values of two operands are equal or not and if the values are not equal, then the condition becomes true. Greater than sign (>); so, this operator will check to see if the value of the left operand is greater than the value of the right operand and if yes, then the condition becomes true. And now less than (<) sign. So, this operator checks if the value of the left operand is less than the value of the right operand and then if yes, the condition becomes true. Now here's the greater than or equal sign (>=).

So, this operator checks to see if the value of the left operand is greater than or equal to the value of the right operand and if yes, then the condition becomes true. And less than or equals (<=). Well, this operator checks if the value of the left operand is less than or equal to the value of the right operand and if yes, then the condition becomes true. Alright, so that's the basic outlay. Why don't we set up some examples using equality and relational operators in Android Studio? So, what do we first do? Create a new Kotlin file. It's all right, click on the package name, select the new Kotlin file option. After selecting the file option here, we'll determine the file name. And I'm going to call it EqualityOperators. So, I'll hit 'Enter' and now I create the main method. So, I write main, select the maina option here, and hit 'Enter'. Alright, so there's our main method created. Alrighty then, let's start to code. So, let's declare two variables with end type. Firstly a and assign 2. Second b and assign 5.

So, the first print method, we use the equal to operator, a == b. Let's copy the print method and paste it five times. Okay, so in the second print method, we can use the not equal to operator in the parenthesis, a != b. Third print method, we'll use the greater than operator, a > b. In the fourth print method, we use a greater than or equal to operator, a >= b. In the 5th print method, we'll use the less than operator, a < b. In the last print method, we'll use the less than or equal to operator, a <= b. Alright, so that wraps that. So, let's run the code and see what we get on the console. 'a == b is evaluated false. 'a != b is evaluated to true. 'a > b is evaluated to false. a >= b is evaluated to false. a < b has evaluated to true. a <= b evaluated to true. So, let's change the value of the variable a to 5. So now a = 5 and b = 5. Why don't we run the code again and compare the results? a == b evaluated to be true. a != b evaluated to false. a > b evaluated to false. a >= b evaluated to true. a < b is evaluated to false. a <= b is evaluated to true. Alright, so what do you think now, my friends? That is equality and relational operators that we use in Kotlin. Alright, so before we play around with that any more, I want to take a short break because in our next video, we're going to talk about conditional operator. Alright, so I'll see you then.

About the Author
Students
86
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics