Nullable variables are an important part of Kotlin and will be discussed in detail. Learn how to use variables in the Kotlin app, including val and var and their differences.
- [Narrator] Kotlin has two ways to declare variables, val and var. Val designates a variable as an immutable type. This means that once a value is set, it cannot be changed. For example, val toolbar equals find view by ID as toolbar. Toolbar is the variable, and as toolbar defines what type it is using the as keyword as Kotlin's way of casting a variable. You may recognize find you by ID as Android's API for finding a view in a layout.
In Kotlin, we try to use val as much as possible, as it promotes mutability. This is considered a safer way to program as you don't have to worry about the side effects of other methods changing a variable. The var type means that a variable can be changed. For example, var username equals Fred. Now you can change it to something like username equals Sue, and like Java variables do not need to be declared if the type is obvious.
For example, val ID equals item dot item ID. It is obvious to the compiler that item dot item ID is an integer and will define ID as one. Also unlike Java, variable types are defined after the variable name. For example, val name string equals Fred. Notice that string comes after the variable name. This makes it easier to find the variables you are looking for.
One of the unique features of Kotlin is nullable types. In Android and Java, the null pointer exception is one of the biggest headaches we deal with. Kotlin can tell the difference between a variable that can be null and one that can't. To declare a variable is nullable, you need to add a question mark after the type. Note that only var types can be nullable. For example, var name two string question mark equals null. To use the variables methods safely, use the question mark after the name, and then cull the method.
For example, val length equals name two question mark dot length. The Elvis operator is similar to Java's, but will take the value or the value on the other side of the Elvis operator. For example, val length two equals name two dot length question mark colon minus one. Length two will be either the length, if there is a length, or negative one if name two is null.
The double exclamation operator will evaluate the variable even if it is null, causing a null pointer exception. For example, val length three equals name two double exclamation point dot length will cause a null pointer exception if name two is null. That's a quick overview of variables in Kotlin.
- Setting up Kotlin
- Converting activities from Java to Kotlin
- Creating an emulator
- Installing and adding Android libraries
- Reviewing basic Kotlin syntax
- Using variables
- Creating functions
- Retrieving images using the REST API from Pixabay
- Layering image views using a FrameLayout and a RelativeLayout
- Building the RecyclerView adapter
- Displaying a high-resolution photo of the image in the RecyclerView