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.
Hello friends. So, in this lesson, we're going to learn assignment operators as promised. So, assignment operators happen to be the most common operators. An assignment operator assigns the value on its right to the operand on its left. So, to give you an example, the equal (=) sign is as simple as sign operator. It assigns values from the right side operands to the left side operand. Plus and equal (+=) is add AND assignment operator. So, it adds the right operand to left operand and assigns the result to the left operand. Minus and equal (-=) is subtract AND assignment operator. So, it subtracts right operand from left operand and assigns result to the left operand. Asterisk and equal (*=) is multiply AND assignment operator. It will multiply the right operand with the left operand and assign the result to the left operand. Forward slash and equal (/=) is divide AND assignment operator. It divides left operand with the right operand and assigns a result to the left operand. What about percent and equal (%=) ? Modulus AND assignment operator. So, it takes modulus using two operands and assigns result to the left operand. So now, let's stew up some examples with some assignment operators in Android Studio. After all, that's why you're here.
So, first, I'm going to create a new common file. Right click on the package name, select the new common file option. And after selecting the final option here, I determine the file name, AssignmentOperators. Alright. Just hit 'Enter', and now I've got to create a main method. It's alright. Main and select the 'maina' option here and hit 'Enter'. That's the main method has been created. Alright, so now, we can start to code. So, let's declare three variables with energy type. First, 'x' and assign '20'. Second, 'y' and assign '15'. And last up, z with the initial value zero. Now, let's assign x + y to variable z. And this is the simple assignment operator. So, let's print the result by using the print method and we may run the program after all the operations. So, what we'll do is we'll copy the code. Change the code with a z + = x. This is add AND assignment operator. So, it is the same as z = z + x. Now, we'll change the code with z + = x in the print method.
So, just copy the code and change the code with z -= x. So, this is the subtract AND assignment operator. So, it is the same as z = z - x. So, we can change the code with z -= x in the print method. So now, let's copy the code again and change the code with z *= x. And, this is the multiply AND assignment operator. And of course, it's the same as z = z * by x. And we'll copy the code again and change the code with z /= x. So, this is the divide and assignment operator. And of course, it's the same as z = z/x. And now, let's copy the code again and change the code with z %=x. So, this is Modulus AND assignment editor. It's the same as z = z % x. That should do it, so let's run the code and watch it in the console. x + y = 35, so the value of z is 35. The value of x is 20. z = z + x = 55. z = z - x is 35. z = z * x is 700. z = z/x is 35, z = z % x is 15, which means that the remainder is 15. Alright, so there you go my friends. In Kotlin, you can use these assignment operators. So, we'll take a short break here, and in the next video, we're going to learn unary operators. Alright. 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.