Introduction to Kotlin | Application of Kotlin Part 5

Introduction to Kotlin | Application of Kotlin Part 5

I have a string parameter.  So I’m declaring the string. And then I’m doing if rx length equals 0, assign a parameter to it. If not, whatever.

I’ve got another case where I’m returning a value. And then I’ve got a switch statement where I’m saying, if it’s case one, if it’s case two.

If not, then if the value is within a specific range, then do this.  And it’s going to say, oh, this is always going to be true or false. But for the case of this demo, it doesn’t matter. So what about Kotlin? Where do we kind of try and improve here?

Well, the first thing that you can see is that an if and a when in Kotlin is an expression. So I don’t– well, it can be treated as an expression or as a statement.

But I don’t have to explicitly declare a variable, assign that variable based on the actual branch. I could just return that as the result of the expression.4


This is a single line of expression. If I have multiple lines, curly braces, then the last line of that branch is going to be the return value.

So here, I’m doing value can be 1, 20, or I can write multiple statements. And then the last value is going to be the return value.

I could even return a string here. But if I return a string, the compiler is going to give me a warning and say, well, conditional branch is returning a type string.

So I’m casting the return type to any. Any is the top level object in Kotlin, meaning that if you want to, you can return a different type per branch.

Why would you want to? I don’t know, but you can do it. Well, there are cases, obviously. Now in the when statements, similar to a case, but we do allow a certain level of pattern matching.

So you can see that I have one, I have two, which we had in the Java case. Then I can have a bunch of values, three, four, five.

I can even check for whether it’s a type. I can even check for whether it’s an arrange and an else branch. I can even call a function, and have the function calculate a value, and return whether that’s a true or false for that statement.

Point being what? It is very similar in code to what you’ve seen in Java, difference being that it’s saving you a lot of pain when you’re writing this code. It’s allowing you to be more concise, more expressive.

And that is essentially what Kotlin is about. It’s not about a single feature that you look at and say, OK, this is cool. But I can do this in Java. You can do everything in Java. And when I say Java, I mean you can do everything in Assembly. How many of you here program in Assembly?

Three people. Awesome. That’s good. Not still, no? Well, yes, still? No? OK. Now another thing that we try to help with is Java nulls.

This is your typical example of a Java null, If customer’s not null, if customer.getName is not null, if customer.getName starts with blah, blah, blah, then do this. Do that. How do we solve this problem in Kotlin? Well, we said we don’t have nulls. We do, but we don’t. Well, we do, but I’ll tell you we– yes, anyway.

March 19, 2019 Ravindra Datir

Leave a Reply

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