Chapter 2 : Working with Variables in Kotlin (Android)

In this article, you will going to learn about variable declaration its usage in Kotlin.

But before moving to current topic, I explained how you can configure your Android Studio with Kotlin in previous article. If you are still pending with that, please read below article first and then continue with this article.

Chapter 1 : Configuring Android Studio with Kotlin

Declaring variables

In Kotlin, everything is object.

In Kotlin, everything is an object. We don’t find primitive types as the ones we can use in Java. That’s really helpful, because we have an homogeneous way to deal with all the available types. – Antonio Leiva

Yes, that’s true and this is super cool feature of Kotlin and reason is already given by “Antonio Leiva”. Let’s declare our first variable.

Declaring first variable

You can simply declare a variable by using var or val keyword and assigning type to object.

Example

Above example will immediately assign value to its object. In example, do you notice anything?
No?
Semicolon 😀

You can also create variable by simply inferring value to object like

Based on assigned value, variable will be created of particular type. Sounds interesting, right!
Below is size occupied by each object you define of type.

Type bit width
Type bit width

You can also create object by using any declared variable and assigning it to new variable.

Output

Constants

var is like general variable and its known as a mutable variable in Kotlin and can be assigned multiple times. val is like constant variable and its known as immutable in Kotlin and can be initialized only single time.

In Java, unknowingly we are using mutable instances and we are facing many issues of object modification at various places. When we are creating any immutable object, its state will remain as it is. If we want to modify it, we require to create another object.

While developing application in Kotlin, it is suggested to use val as much possible. These objects are also thread-safe as their state won’t affect in which thread it is currently.

String Template

Working with Strings in Kotlin is too easy.

By using $ (Dollar) sign & object name in string template will directly fetch value of object. You can also treat String as array and assign any particular character to object.

Null safety

As in my previous article, I stated that Kotlin is Null safe. The reason behind it is Kotlin is not allowing user to assign NULL values to any object until and unless you explicitly tell Kotlin that object might contain null then only it will allow you to define null object. You can do it by using “?” (question mark) after type.

Type cast

In Kotlin, there is no automatic type conversion like Java. You require to specify to which type you want to cast.

Summary

In this article, you learnt how you can declare variables of different types, what is difference between var and val keywords, how you can cast variables from one type to another type.

Protected by Copyscape

Share on Facebook37Share on Google+10Tweet about this on TwitterShare on LinkedIn13Share on Reddit0Pin on Pinterest1Share on Tumblr0Email this to someone