Basic of Kotlin to get started in android Part-2

In my last tutorial, we have learned the basic introduction about Kotlin. Kotlin is inspired by C#, Groovy, and Scala language. I recommend you to please check my last post-Basic of Kotlin to get started in android part-1.

Google I/O 2017 announced that Kotlin will be the official language for Android development. This is great news for JetBrains team and all Android developers. Finally, we got the new awesome language to use in the android-building.  Thanks, Google to take care android very strongly and to frequently update android features to provide the better user experience.

OK let’s start to understand this new awesome language kotlin.

Variables in Kotlin

Variables in kotlin are similar to Java in their scope. Variable in Kotlin is mostly preferred the immutable because of its thread safe. One interesting thing in kotlin is that we do not need to declare the object type as long as the compiler can infer it.

So we need to write var and val depend on the type of the variable we want to generate. Even Kotlin also provide an option to define the variable type explicitly. Kotlin automatically typecast as those.

One more interesting thing in Kotlin is that we do not need to write the semicolon at end of the statement. All those are boilerplate code in old programming which is removed in Kotlin.

Here is the few example to understand.

Functions in Kotlin

Function in Kotlin is similar to method in Java.  Only thing is different in kotlin is method represents predefined fun keyword. Every fun is by default public in Kotlin. A function could be return type and not be, it depends on our requirement. Everything is similar to in java for example argument parameter, the only syntax will differ in Kotlin.

Let’s see all different type of function we can define and how to call one function inside the function?

Conditional Statement 

If I am talking about conditional statement then I am sure you understood about the if -else, while, do- while, and for the loop. One thing I did not mention here switch condition. So switch condition is no more with Kotlin. In place of the switch, we can use when condition. Let’s see with an example how we can use all these conditional statements in Kotlin?

Let’s check first if-else condition in Kotlin. It is almost same like Java.

But most preferable in Kotlin is like that.

Let’s see how we can use when condition which is replaced with switch? Kotlin has improved a lot with when condition.

Even we can give the range also and any object type to check in cases.

and many another way Please check kotlin official site. Ok, Let’s see the while and do- while loop in kotlin.

This is while loop now lets’s see do- while loop.

Now let’s see how we can use for loop in kotlin. For loop is mostly used for iteration. This is the best iteration of the list of items. In Kotlin we can iterate it in two different ways.

Great. Awesome Kotlin.

Wrapping up – This is all about the variable declaration, Function and Conditional statement. You can do much more practice with these concepts in deep. I recommended going through the Kotlin official site for more update. In my next tutorial, we will learn other features about class and Object. Here is the link of Basic of Kotlin to get started in android part-3.

Ok Please do subscribe your email to get the updated newsletter of this blog and if you like this post then do not forget to share and comment below.

Happy coding 🙂

I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com

Leave a Reply

Your email address will not be published. Required fields are marked *