Check out a free preview of the full Introduction to Kotlin and Android Development course

The "Kotlin Basic Expressions" Lesson is part of the full, Introduction to Kotlin and Android Development course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano walks through some basic expressions in Kotlin, including global expressions, variables, booleans, semicolons, printing to the console, correct casing, and conditionals. A student's question regarding using multiple cases is also covered in this segment.

Preview
Close

Transcript from the "Kotlin Basic Expressions" Lesson

[00:00:00]
>> It's time to write some Kotlin code. So in the repository, there is a public folder. And in that folder, we have assets, okay? It's a zip file. If you unzip the file, you will get some PNG that we will use in a couple of minutes. And also, you will get into a kotlin 101 folder.

[00:00:23]
Where we're going to do now, is to open Android Studio into that folder. It's not actually an Android project. This is actually a couple of files with extension, kts. By the way, Kotlin files are kt files. Kts, what's that? The SL from scratch, it got a scratch file.

[00:00:43]
What's an A scratch file? It's actually a file to play with Kotlin without actually making an App, okay? That's kind of idea. You can open that by the way if you want in VS code or any other IDE that you want in this example only. For Android development, we will actually need Android Studio.

[00:00:59]
But for these files, you can open this in Android Studio if you want, just to see and play with [INAUDIBLE]. In Android Studio, you can open and then you go directly into the folder. This case I have it into assets, and write Kotlin101. It will look like this.

[00:01:24]
So you have a list of files, and then with the tip of the day that I don't want, and then the code on the other side. So, here we are going to just see some code and also if you want, you can run Kotlin go. That is like a playground.

[00:01:44]
It's a place to play with Kotlin without actually making an App, okay? So basic expressions, Kotlin code looks like JavaScript or any modern language when you first see it. So, if I throw you some Kotlin code, you will understand most of it without even doing any explanation. So, I will just sham directly into the differences and really quickly over the things that are usual, where all similarities to other language.

[00:02:12]
So, Kotlin looks like any other language. For example, semi colons, semi colons out they are, but they are optional. And I can say that 99% of the time, you're not going to use semicolons. You use semicolons only when you have two expressions, two sentences in the same line.

[00:02:31]
And that's not something you wanna do actually, okay? So, your grade variables as you can see really quickly with bar, so that's actually pretty straightforward as with JavaScript. But we'll get into data types and all that stuff in a minute. Boolean conditions, they need parentheses. So for example, if you're coming from other languages.

[00:02:54]
Python, Ruby, Swift, here have in mind that this is like JavaScript or C or other C based or C syntax languages. You need parentheses on if, on while, on for. Print and print line, print will send a message to the console, print line will send a message and grade the new line.

[00:03:16]
One big important thing here, when we are using print to talk to the console, the console is not the phone, okay? The console is an invisible part of your App, of the operating system. And Android does not support printing without a new line. So, actually when you do print, it goes to a buffer until you do at least one print line.

[00:03:43]
When you do a last print line, you will see that on the screen. Meaning that if you're sending print messages, you're not going to see those messages in the console, until you send a newline character or a print line, okay? Have that in mind. When you're doing console Koltin applications, you can use print and it works, okay?

[00:04:01]
But on Android, we are running on different operating system with its own rules. So, in terms of style guidelines, variable function and package names are using camel case. What's a package? Similar to Java package or similar to namespaces in dot net or PHP, it's a way to group co, under a name, okay?

[00:04:25]
All of them are using camel case. So, we start with lowercase and then we go uppercase, you know the deal. Constant names, okay? They're using UPPER SNAKE CASE. That is, for example, this one Max bank. We will talk about this in a minute, like conspire, what is that?

[00:04:42]
No worry for that. And data types, they use title case, as in JavaScript, Java and many other things like a class an object that we don't know what that is yet, but let's say that, it's a type interfaces. Also objects that are represented in singleton's with 10, to use also title case.

[00:05:04]
So we start with uppercase. These are just guidelines, of course no one is gonna enforce you these ideas, okay? Just guidelines. Something interesting is that conditionals are expressions. So, you can call a function and put an if inside the function, okay? And this is different to other languages.

[00:05:29]
Probably you are used to like I know, less than 2, else more or equals to 2, something like that. This is perfectly valid in Kotlin. So, this is actually replacing the ternary operator that probably you're used to. The ternary operator is not available here, you need to use if instead.

[00:05:52]
It's just a ternary operator with a different syntax, okay? Makes sense. We don't have a switch, but we do have a when, that it's acting like the switch, okay? And it's even more powerful. We use when and then different cases. On each case we use this one known as a thin arrow, dash and greater than.

[00:06:14]
And we can have one line, we can have a code block and there is no need for a break, okay? There is an automatic break here going on, we can also use ranges, multiple values and even we can use operators, for example in this case, asking about the data type of something.

[00:06:36]
else it's what we are used to as default on other languages, okay? It's just that when you get used to that not a big deal. Loops,
>> It goes through all of them, right? So, it can go through multiple cases, right? Question so, what if you wanna go with multiple multiple cases?

[00:06:53]
By default it's not going through multiple cases, okay? Because when you they realized when they created code in the language that most of the time, we are using the switch or their use case the when, for exclusive options. But if you want, you can request a for through.

[00:07:11]
So the for, so it's the other way around, instead of breaking, you are going to say that you wanna continue on the next one, okay? And in that case, you use for through, so you can request explicitly that you wanna move to the next case, okay? Loops, we have the standard while, so it's just while, as you know, okay?

[00:07:33]
Don't do this at home, but well true. And the for, we only have for in as an option. If you wanna use the all for I equals zero, I less than, I++, you can use the ranges, like this, a range is some kind of a little collection on the fly, okay?

[00:07:53]
It's not exactly collection, but it seems like that here, okay? So, in this case zero to 10 without 10, okay? So, these will print 10, 0 to 10 ,sorry 0 to 9, okay? Simple, that simple.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now