From the course: Build Spring Boot Apps with the Kotlin Programming Language

Object equality and the when expression

- [Instructor] There's another improvement on the if statement that we need to look at, but before we can get to this we need to talk briefly about object equality. Let's recap that in Java, if we write object1 == object2 this is testing for reference equality. That is the double equals symbol is a test to see if these are the same object if the two variables are pointing to the same single instance of an object in memory. If we want to check for value equality, that is whether the content of object1 matches the content of object2 even though they might be different objects in memory, then we use the dot equals method. In Kotlin, equals equals tests for value equality. In other words it's a short-cut for the dot equals method. In Kotlin you can use dot equals as well, but of course it's quicker to type the double equals. If we want to check for reference equality in Kotlin, to check if they're the same object in memory then we use three equals symbols, or the treble equals. So now that we have that let's go back to our if statement. I'd like to write another function in our person class that provides a category for their favorite color. So something like..., let's do this one down here as a function. And we'll call it getColorType. And it's going to return a string. What I want to do is something like... Well let's get the uppercase color because that's a string first of all, and let's store it locally. So let's say color equals getUpperCaseColor. Then we can say if the color is an empty string we're going to return the keyword empty. In fact let's put the return word in there "else". And then I want to do "if" with a few different options in here. Let's say if the color is red, or the color is blue, or the color is green then we'll return the word "RGB". And then let's have a final else statement in here which means we can return "other". Okay so I've missed off one of my equals there and now that will compile. That should be fine. And hopefully what you're seeing here is that we are comparing strings using the double equals syntax. If we were writing this in Java we'd have to use dot equals. But that's not all. Kotlin has a short-hand way of doing this. Now it is based on the switch statement but there are a few important differences. The keyword, rather than switch, is when. And the syntax looks a bit different. So let's convert this to use the when statement. I'm going to just comment this out for a moment, and write it below. So we're going to return and then when our color, if you like, that's what we're doing our switch on, and we'll open a code block, but we'll say when our color is an empty string and we use the arrow symbol to provide the return value. Then we're going to return empty. Next line down, when our color is red or green, or blue, then we're going to return RGB. And if it's else, we're going to return other. Now that is a lot more concise than this version. As I said it's based on the switch statement but it's significantly different syntax. We use this arrow symbol and there is no fall through. What I mean by that is that when you use the switch statement in Java you have to put a break at the end of each section of code. In Kotlin you don't. The way the when statement works is that as soon as a match is found the return value is obtained and the when stops running. And as you can see if you've got multiple values to evaluate against, you simply provide them as a comma delimited list. Now I'm aware that there are a number of people who are not overly keen on the switch statement in Java, however the when statement in Kotlin is very idiomatic. And you should absolutely be using it to avoid multiple if-else statements whenever you can. I think the reasons why are pretty obvious. It's very concise, it's very readable, and you don't have the risk of missing the break statement and therefore getting the wrong results like you do in Java. So I think that's quite nice. I'm just going to remove my commented out code to tidy this up. Okay, well we've covered quite a lot in this chapter but that does really cover the if statement, the when statement, and object equality in Kotlin. In the next chapter we're going to be looking at looping. And in order to do that something called ranges. So when you're ready I'll see you in chapter nine.

Contents