Arrays and Null References

Tim Buchalka
A free video tutorial from Tim Buchalka
Java Python Android and C# Expert Developer - 925K+ students
4.6 instructor rating • 12 courses • 925,542 students

Learn more from the full course

Kotlin for Java Developers

Use your Java skills to learn Kotlin fast. Enhance career prospects and master Kotlin, including Java interoperability

16:41:26 of on-demand video • Updated January 2021

  • Create Kotlin programs
  • Understand the differences between Java and Kotlin
  • Understand how Java and Kotlin programs interoperate
English (bright tones) (keyboard typing) Instructor: Okay before we leave the topic of null references, I wanted to briefly revisit arrays. Now you learned how to declare arrays with initial values, and you learned about the empty array function although I said I didn't know a use case for it, but at least you know about it. But, I also said that there's a way to create an array of nulls, and we'd come back to it later. And now that you know about null references and how Kotlin handles them, now we can talk about creating arrays of nulls. Now maybe now you understand why the array constructor wants a size and the initial values. Because think about it, if you only give it the size and it creates an array of let's say of size five, and none of the elements are set, then that means that the elements are null, and of course that could result in null pointer exceptions and Kotlin doesn't want that. So this is why, if you want an array of nulls, you have to explicitly ask for an array of nulls using the array of nulls function. Now, when it comes to the special primitive arrays that we talked about, you know that they're initialised with what makes sense for whatever type you're using. So for example, with int all of the elements will be initialised as zeros. So when you're using those special primitive type arrays, there is no danger of getting a null pointer exception because the compiler will initialise those arrays for you. But when you're dealing with arrays of objects, of course there is the danger of getting a null pointer exception if you don't initialise anything, and so that's why the array of nulls function is used because that tips off the compiler that you have an array that's basically filled with nullable types, because that's essentially what you're going to be creating. So let's take a look at an example. So we'll say val, let's say nullable ints, because we're going to create an int array, not the special primitive int type array, equals and we'll say array of nulls, and we'll say it's of size, well actually we have to tell the compiler the type, can't tell, and we'll say of size five. And what this will do is create an array of int, of size five, and all of the elements will be initialised to null, so if you want to create an array to hold an object type and you don't know what the initial values are, you're gonna fill them in later somehow, this is how you can do it. So to confirm that we've got an array of five nulls basically, let's print this out. For I in nullable ints, and we'll just print line I, and let's see what we get. And I'll just comment all these things out so we're not seeing stuff we don't want to see. Let's run. And we'll see that we get five nulls. So by using the array of nulls, you can get five nulls here. Now of course what you can do with these elements right now is going to be limited, so let's take a look at, let's just say I don't know, nullable ints three dot, and we'll see what comes up and you can see that we do have some functions available to us, but most of them, they're not because the compiler knows this is a nullable int, and so most of the stuff isn't available. You'll notice that two string is available, and it'll say for any question mark, see so the compiler knows that this could be null, and you'll see that when we call it, we'll just get null printed out because essentially it'll do what the safe operator does and it'll evaluate to null when two string is called at a null value. Let's try it just for fun. And we might as well print it. So you'll see that we don't get any errors even though this is null right now, and if we run, we'll get a six null down here and that's null coming from this print line. Now, we can remove the question mark from here. That's not gonna cause an error because the compiler knows that because you're calling the array of nulls function that you're creating an array of nulls. So under the covers you're still getting a nullable int and you can see that if we pull this away and hit the dot and you'll see that everything is still greyed out for us. We still can't use any of the usual integer functions because this is actually a nullable type, even though we didn't specify that, compiler is smart enough to know. So I just wanted to show you how you can create an array of nulls. So that's it about null references. We'll probably touch on them again when it makes sense later in the course. Now I want to end this section by saying that the verbose ways of writing code are all valid code. When we've taken a look at null references we've seen the long hand for writing stuff, and we've seen the short hand for writing stuff. You can write, if something is not equal to null then, and sometimes you'll have to do that. I mean for example if you have to check that three things aren't null before you execute something, then you may not be able to use the safe operator, you may have to go ahead and explicitly spell it out in a condition. For example, if a and b and c and d and e all have to not be null before you do something, there's no way to use the safe call operator or the Elvis operator to check that all five conditions are met. You'll have to type them out. So I just wanted to make sure you understand that the verbose ways of checking for null are all valid in Kotlin, you're not gonna get a compiler error as we saw, but they're not always in the spirit of Kotlin, which strives for concise code. So, you really only wanna write the verbose way of checking for nulls by explicitly doing it if there's no other way to do it, if you're in a situation where you can't do it using the safe call operator and you can't use the Elvis operator. So in those situations, just go ahead and do it the verbose way. I'll see you in the next video.