Introduction to Kotlin Application of Kotlin

Introduction to Kotlin | Application of Kotlin Part 2

By the way, val is something in Kotlin, which means immutable, read only.

It means that I can’t reassign that. If you want to reassign something, you declare it var. My rule of thumb is don’t do it. Just declare it as val and work towards having read-only variables.

And then I can do is here, and print message, So I can pass in one parameter. I can pass in two parameters.

I can pass in three parameters, And the good thing is that you can see that this is actually  this is  Intelliger  giving me that the name of the parameter.

But we actually have named parameters in Kotlin too. So if I want to pass in the suffix first, I could just put suffix, and then pass in the suffix.

And then here I would say the prefix, and pass in the prefix. And then this would become the actual message.

I can alternate the order in which I want to pass in the parameters. And this is useful, of course, not only for alternating order, which why would you want to do it?

But anyway, but it’s useful when you have multiple functions, probably legacy that has 200 parameters, and you’re playing a game of true and false.

Introduction to Kotlin Application of Kotlin
Introduction to Kotlin Application of Kotlin

So you can start to name those things. Or you could refactor. But why bother when you can hack things together. Now Kotlin does have classes, as I’ve shown you.

I’ve created a class. And one of the typical quintessential examples that we show is a class in Java. So let’s say customer job, customer type, ID type, customer Java, split vertically, and let’s show customer Kotlin.

OK, there you go. Now, and then again, you’re going to say, oh, big deal. Any ID generates this for me. Yes, it does. But the ID doesn’t maintain it for you.

And the ID doesn’t come and say to you, oh, don’t look at that, Hadi, because that’s just boilerplate code that I’ve generated for you.

I don’t know if someone’s gone and modified all of that code. So all of this, essentially, is declared with a single line of code.

And the data class over here, when I add the data modifier to it, what essentially that is doing is providing me with some new functionality.

Namely, it’s giving me the equals, the hash code, the to string, and the copy, which we’ll see in a moment.

What you’ll also notice here is that essentially, I’m doing the whole property declaration and constructor also in a single line.

So here, I’m declaring three properties. One of them is read-only. The other two are read-write. And that, in turn, is also creating a constructor.

That’s the short syntax. You can also do this in a longer syntax. One thing to notice, if you’re not familiar with Kotlin, is that we don’t have fields as such.

We just have properties. And then you access these properties with accessors. There’s no get or set. Or there is, but it’s under the covers for you. When you talk Java to Kotlin, you get the Java to Kotlin and Java to Kotlin.

There you go, or Kotlin to Java, you get the idiomatic approach in each language. But if you’re just– you’re working with Kotlin, you can just use them directly.

Also Read : Introduction to Kotlin Part 1

Also Read : Introduction to Kotlin Part 3

So I can say, for example, val customer equals customer Kotlin. And then this will be 1 a b. And then do customer.email equals something. So a lot of boilerplate code you’re saving there.

March 16, 2019 Ravindra Datir

Leave a Reply

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