Operators in Kotlin
The course is part of this learning path
This course takes a deep dive into Kotlin operators, giving you a practical understanding of how to use operators in your code.
This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.
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.
Alright. Well, hello my friends. So, in this lesson, we're going to talk about the Unary Operator. The unary operators require only one operand. So, they perform various operations such as incrementing and de-incrementing value by one, negating an expression, or inverting the value of a Boolean. You can see how important that would be. So, let's have a look at these operations, shall we?
Well, there's the +. So, this is the Unary plus operator. Now, it's used for giving positive values; match. But it's only used when deliberately converting a negative value to a positive. And this is the Unary minus operator. And it's used for negating the values, double negatives. So, the Increment operator a ++. It increments the value by one. -- the Decrement operator, it decrements value by one. And the ! , so this is a Logical Complement Operator. It inverts the value of a Boolean. So, why don't I show you a few examples of unary operators in Android Studio and you'll see how useful they really are?
So first off, I want to create a new Kotlin file, so I'll right click on the package name and select the 'New Kotlin File' option. After selecting the 'File' option here, I just determine the file name as UnaryOperator, hit Enter. Okay, so now I've got to create the main method. Write main and select the 'maina' option here, hit Enter, and that way the main method gets created. Alright, so why don't we start the code? Let's declare two variables. First off, number with Double type and assign 7.6. Second is check with Boolean type and assigned true. And now, on the first print method, we're going to use the unary plus operator. So, let's copy the print method and paste it four times. And for the second print method we can use the unary minus operator. Third print method we'll use the increment operator. Fourt print method we'll use the decrement operator. And for the last print method we'll use the logical complement operator. Alright, so that's that, let's run the code.
Now, remember the value of the number variable was 7.6. So, what do you see in the console? +number equals 7.6, so there's no change. -number equals -7.6, so it inverts the sign of a number variable. ++number equals 8.6, so it has incremented the value by one. --number equals 7.6, decrements value by one. !isCheck equals false. So, the first value of isCheck was true. Remember? So, this inverts the value of a Boolean. Now in Kotlin, we could also use a ++ and -- operator as both prefix and suffix. However, there is a significant difference while using increment and decrement operators, for prefixes and suffixes. So, let me just show you by example. So, I want to declare a variable result with the double type and assign 4.7 to it. Now, in the first print method we use the increment operator as a suffix and write result++. And in the second method, we'll write only result. And for the third method let's use the increment operator as a prefix and write ++result. And for the last print method, we'll write only the result again.
Alright, so that's all that. Let's run the code and see the results in the console. See so, when the first print is executed the original value is evaluated first. Result is increased only after that. First, the result variable that is 4.7 printed to the console and afterwards increased the result value so we got 4.7 as an output. Also, the result value is 5.7 now. So, the second print gets executed, the increased value 5.7 is printed. However, when the third print is executed, the result is increased by one first, before it's printed on the console. So, the value of 5.7 increases by one and the new value of the result variable becomes 6.7. So, then the value of the result variable is printed to the console like that. And of course, when the last print is executed, 6.7 gets printed. Alright, so very quickly and very simply those are the unary operators that we use in Kotlin. So, we'll take a short break here and in the next video we're going to learn about equality and relational operators. Okay, so I'll see you then.
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.