Operator Precedence 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

Hello, my friends. So, in this lesson, I promised you that we're going to learn about the Operator Precedence. It really is important though to know Kotlin interprets the symbols that you use in order to perform specific operations and in what order it's going to interpret them. Otherwise, we could just write an application with one result in mind and receive an entirely different result. So, as you can see on the table, the priority column is most important because it defines the strict order, which Kotlin interprets the symbols displayed in the operator column, okay, let me highlight strict order. An operator higher in the table always takes precedence over an operator that's lower in the table.

Now, there is a very important point in this part, if you want to change the order in which an expression is evaluated, you'll have to use parentheses. So, in fact, any part of an expression enclosed in parentheses is evaluated first, you knew there was a trick waiting for you, alright. So, let's make a few examples with conditional operators in android studio. So, first I want to create a new Kotlin file so, I'l right click on the package name, select the new Kotlin file option, after selecting the file option here, I'll name the file and we'll call it operator precedence. So, after I hit the enter key, now, I've got to create the main method. Main select, main a option here, hit enter. Okay, so there's our main method. So, here I'm going to declare a variable result with integer type and assign 5 + 2 * 4.

Okay, so, let's print the result value by using the print method. All run a code and as you can see the result is 13 not 28 because the precedence of the multiplication operator is higher than the additive operator. See, I'm not fooling around. So, here multiplication is performed before adding and that means the result value is 13. Alright, so if you did see that one coming, let's do another example. So, let's assign in the parentheses 5 + 2 * 4 to the variable result, copy the print method and run the code. Now, the result's 28. So, here the expression enclosed in parentheses is evaluated first just like I promised. And you see that the multiplication is done. Okay, so, first five plus two equals seven is done and seven is then multiplied by four and the result equals 28. Now, are you ready for a new example with unitary operator precedence?

So, let's declare four variables with an integer type. First x assign 8; secondly, y assign 4; third z assign 2, in the last sum assign 0. So, let's assign x + -- y + -- z to the variable sum. And we'll copy the print method. So, now let's run the code and as you see the result is 12 because the operator precedence of the unitary operator is higher than the additive operator. Things to keep in mind, you got room in there, don't worry, alright, so, the operator precedents used in works like this all the time. So, we'll take a short break here and next time we're going to talk about the range two operator, so, I'll see you in the next video.

 

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