Search
  • David Cruz

Kotlin keyword's: var, val, lateinit var and lazy

If you are starting into the programming or you are an experienced developer trying to learn the differences between the different variables, to improve your code or to reply interview questions, in this series of blog posts you will learn about what are the differences between the different variables you can find in Kotlin. I will try to make it short but this topic is really long so. Let's start!.


VAL

the first keyword and one of the most common in kotlin is val. By default, you always will be using val keyword except you have special need like you need change the value at some point in your code

val heightInCm = 168 or val heightInCm: Int = 168

This both variables are exactly the same one, the only difference is visual. The first variable is Integer because we set an Integer number as a value if you change the values of this variable for a String like "hundred sixty-eight" it will not cause any issues. From the other side, the second variable is implicit an Integer if we try to change the type it will show an error.

VAL variables MUST have a type and a value when is declared and is immutable and final, means it cannot be changed and the value assigned is the only one will have.


Remember: Always use val keyword because is immutable (the value will not change) and final.



VAR

This Kotlin keyword it shares all attributes of val keyword however, the only difference is var is mutable and non-final. here are some examples:

mutable and not final it means it can be changed for other value of the same type and the value assigned is not the last as it can change anytime.


if your variable at some point will be null, you can declare the type with a question mark, like the last example on the previous Gist.


Remember: use var keyword ONLY when you need to change the value at any point in your code.



LATEINIT VAR

is like a var keyword, but it MUST be initialised at some point in the code and of course before been used, with lateinit var if you try to use the variable before initialise it will crash. Sounds obvious, but it will crash even if you try to check null. there is not null if is not initialised, it will crash so is your responsibility to be careful with it.



LAZY

this keyword means lazy initialization and basically means this variable will not be initialised except it is used in your code, it will be initialised once and will you can use the same value in your code. example:

val name: String by lazy { val nameString = "David" }

Lazy is a function that takes a lambda and returns an instance of Lazy which can serve as a delegate to implement a lazy property. in other word and for simplify:

The first call to get execute the lambda that is passed to lazy remembering the result, the following get calls only gets the same result previously remembered.


I hope this blog post helps you to understand the advantages of using the most appropriate keyword for your variables and initialisations.

27 views
I Sometimes Send Newsletters

© 2019 David Cruz

Disclaimer: Opinions expressed on this web and blog are my own and do not reflect any of the companies I worked in the past or currently.