Using Operators in Kotlin

Arithmetic Operators in Kotlin Android
Overview
Difficulty
Intermediate
Duration
46m
Students
16
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

Hello friends. All right. So, in this video, we're going to learn about arithmetic operators in the Kotlin programming language. So, Kotlin provides operators that perform addition, subtraction, multiplication, division, and remainder. So, these are basic mathematical operations that you know and love.

+ is the additive operator, it adds two operands. - is subtraction operator, it subtracts right operand from the left. * is the multiplication operator, it multiplies two operands. / is the division operator, it divides left operand by the right one. Type of division result is float by default. % is the remainder operator, so this symbol might look new to you, it divides one operand by another, and it returns the remainder as its result. All right. So, let's dive in little bit deeper to understand these arithmetic operators with some examples.

So, we'll open up Android Studio. Now, first I'm going to create a new Kotlin file. Right click on the package name, select the new Kotlin file option. Now, after selecting the file option here, I'm going to determine the file name to be ArithmeticOperators. Cool. So, I'll here 'Enter'. So now, I am going to create the main method. Write main, select the main option here, hit 'Enter'. So, this main method has been created. So now, I am going to declare three variables with double types. First, number1, that is sign 8.4. Second, number2, a sign 4.2. Last, result with initial value 0.0. So, first, assign number1 + number2 to the variable result. And then, let's show the result value by using the printIn method inside the double quotation marks, so I'll write number1 + number2 after the plus sign, I write the result.

But the plus sign we use here and the plus sign we use to add number1 and number2, you see how they're different from each other. Here, we use the plus sign to concatenate two different string expressions, like I mentioned earlier in our previous lessons. So, here's two mathematically sum two different values. And we may run the program after all these operations. So, let's copy the code, change the operator with the subtraction operator, and I'll put the - sign in here and change the operator in the println method. Put the - sign in here too, all right? So, let's make changes for multiplication. So, put in an * in here and I'll put the * sign here. And we can make changes for division, so put the / sign here, put the / sign here. And then finally, why don't we make changes again for the remainder operator?

All right. So, that's all for now. Let's run the code and let's watch the console. number1 + number2 = 12.6. number1 - number2 = 4.2. number1 * number2 = 35.28. number1 / number2 = 2.0. number1 % number2 = 0.0, so the remainder is zero. All right, so there you have it my friends arithmetic operators used in Kotlin work out to be just like that. That's all. So, we'll take a short break here. And then in the next video, we're going to learn about assignment operators.

About the Author
Students
170
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