Introduction to Kotlin

Introduction to Kotlin | Application of Kotlin Part 1

Introduction to Kotlin | Application of Kotlin Part 1

Welcome to this session on Kotlin. Now I’m assuming the majority of you here have probably heard of Kotlin, maybe not played with it, have seen it a little bit.

If I got that right, great. If I got it wrong, just keep it to yourself.

And we’ll start. I’ll show you a bunch of different things with Kotlin.

You can also visit Kotlin official Website.

Also visit Google Android Developer Website.

I’ll compare it a little bit to some other programming language. Namely, we’ll pick Java today to give you a feel of where and what we try and provide you with value. And just to get started, this is my main function.

So you can see it up there. And I can write hello. And then I can run this. And that’s as simple as it gets. So to start with, you don’t have a public static void main, and you don’t have a static class. You just have basically top level functions.

As I always say, it’s like JavaScript, but in a good way. And you can basically define your functions as you like. So I can say function, for instance, print message, and then have a parameter here of type string, and then print line, and then the message.

And I can do string interrelation here and do whatever I want. Now notice that there’s no return type. By default, it’s unit. And you can omit it if you don’t have a return type. And functions, again, conciseness of Kotlin.

Introduction to Kotlin
Introduction to Kotlin

So what we’re trying to do is make functions as simple as possible, and as easy as possible to write, and as concise as possible to write.

And if a function returns an expression, returns a value, you can just write that as what we call a single expression function.

So essentially, if I do a sum, for instance x int y int, I can just write that as x plus y. I don’t need to explicitly put the return type. I don’t need to open the curly braces, close the curly braces. Just write it as simple as that.

Also Read : Introduction to Kotlin Part 2

Also Read : Introduction to Kotlin Part 3

Then, of course, you can call these functions anywhere you want. Print message, something. Now how does this help in terms of what you already have? You’re like, OK, the syntax is a little bit more concise, and I write fun instead of not fun.

And I write unit instead of void. But what does that give me? So let’s take a look at, for example, overloaded functions in Java, This is overloaded methods in Java.

And I’m going to split that vertically. And now take a look at overloaded function in Kotlin, So you can see that this is already somewhere where it’s cutting down code. I don’t need to do these overloading methods anymore in Java.

Obviously, I’m putting this outside in its own top level function. But this could actually be a member function of a class. So don’t worry about saying, where is the class? The class is somewhere here. The class is here. There you go.

Now you got the class. There. Now you got the class. Happy? There. People think, oh, there’s no classes in Kotlin. Yes, there are. Anyway, so already cutting down boilerplate code. What am I doing?

Essentially here, I’m just passing in optional parameters. Simple as that. Why do I have to have all of this, with all of this code over and over again if essentially, I’m doing the same thing, but with different number of parameters.

And then, of course, I can call this, and I can say, for example, val is here. We’ll now create a class.

Also Read : Introduction and application of kotlin part 4

March 15, 2019 Ravindra Datir

Leave a Reply

Your email address will not be published. Required fields are marked *