Basics Of Kotlin - JVM Implementation

Hello Guys, we already knew that Google announced that Kotlin is a new first class language for Android Development. Kotlin is now an official language on Android. It's expressive, concise, and powerful. Best of all, it's interoperable with our existing Android languages and runtime.

Kotlin

Introduction

Hello guys. We already know that Google announced that Kotlin is a new first class language for Android Development. Kotlin is now an official language on Android. It's expressive, concise, and powerful. Best of all, it's interoperable with our existing Android languages and runtime.

You can try Kotlin by downloading Intellij idea Community Edition or using Kotlin Online Compiler provided by the Team JetBrains.

In this article, we will learn the basics of Kotlin like variables, constants, conditions, functions and etc. The following topics are to be covered in this article.

Functions

The following snippet shows the Hello World Program in Kotlin and it is similar to the Hello World Program in Java. 

  1. fun main(args: Array <string>) {  
  2.     println("Hello, world!")  
  3. }  

Here the function is denoted by funfollowed by function Name with passing arguments if needed then return type if the function returns data. The syntax of the Program is

  1. fun functionName(arguments: Datatype) : returnType {  
  2.     println("Hello, world!")  
Variables and Constants

The Following Snippet shows the How to use Variables and Constants with Datatype in Kotlin.

  1. Variables
    Variables in Kotlin are denoted by the keyword varand the variables can be re-assigned as like in any programming languages.
    1. var a = 10    
    2. a = a + 10    
    3. println(a)    
    Constants
    Constants in Kotlin are denoted by the keyword “val” and the constants cannot be re-assigned as like in any programming languages. If we did, then the Kotlin compiler will throw an error as “val cannot be reassigned”
    1. val x = 10    
    2. println(x)    
    3. var a = 10    
    4. a = a + 10    
    5. println(a)    
Concate Variables or Constants

The variables or constants can be concatenated using plus symbol as with any programming language.

  1. fun main(args: Array<string>) {  
  2.     var a = 10  
  3.     a = a + 10  
  4.     println(a)  
  5.     val x = 10  
  6.     println(x)  
  7.     var str : String = "Android"  
  8.     println(str)  
  9.     // Concate String Values  
  10.     println(str+"Mad")  
  11.     println("${str}Mad - Kotlin Series")  
  12. }  

The Datatype assigned by adding datatype followed by variable or constant's name.

  1. val x : Int = 10  
  2. println(x)  
Conditions

The Following Snippet shows the how to use conditions like if else, switch case in Kotlin.

  1. if else
    If else conditions are similar to java. But, we can assign he output of the conditions as like ternary operator.
    1. fun main(args: Array<string>) {  
    2.     var arun = 10  
    3.     var dharun = 15  
    4.     var elder = if(arun > dharun)"Arun" else "Dharun"  
    5.    
    6.     println("Elder is ${elder}")  
    7. }  
  1. When
    Like Java, Kotlin doesn’t have switch statement. Instead of switch, it has when statement. The following statement shows the implementations of it.
    1. fun main(args: Array<string>) {  
    2.     var elder = "Dharun"  
    3.     when(elder){  
    4.         "Arun"->{  
    5.             println("Elder is Arun")  
    6.         }  
    7.         "Dharun"->{  
    8.             println("Elder is Dharun")  
    9.         }  
    10.         else->{  
    11.             println("No Answer")  
    12.         }  
    13.     }  
    14. }  
Loops

The Following Snippet shows the How to use loop conditions like for, while in Kotlin.

  1. For loop
    The following code shows the example of “for loop” in Kotlin.
    1. // For Loop  
    2. fun main(args: Array<string>) {  
    3.     for(i in 1..10){  
    4.         println(i)  
    5.     }  
    6. }  
  2. While
  3. The following code shows the example of “while loop” in Kotlin.
    1. // While Loop    
    2. fun main(args: Array<string>) {    
    3.     var x = 1;    
    4.     while(x<=10){    
    5.         println(x)    
    6.         x++    
    7.     }    
    8. }   
Arrays and Lists

The Arrays can be created by arrayOf keyword in Kotlin. It will accept any type like Object in Java. The following code shows its implementations.

  1. fun main(args: Array<string>) {  
  2.     var x = arrayOf(1,2,3,4,"Androidmads");  
  3.     for(i in x){  
  4.         println(i)  
  5.     }  
  6. }  

It can be controlled by Datatype as in Java. The following code shows its implementations.

  1. fun main(args: Array<string>) {  
  2.     var x = arrayOf<String>("Android","Mads","Kotlin","Series");  
  3.     for(i in x){  
  4.         println(i)  
  5.     }  
  6. }  

The Lists can be created by “listOf” keyword in Kotlin. It is similar to “arrayOf”. The following code shows its implementations.

  1. fun main(args: Array<string>) {  
  2.     var x = listOf<String>("Android","Mads","Kotlin","Series");  
  3.     for(i in x){  
  4.         println(i)  
  5.     }  
  6. }  
Classes

The Following code shows how to declare / create objects for class in Kotlin. For Example, Create a class and named it as "Model.kt"

  1. class Model {  
  2.     var name : String = ""  
  3. }  

The object call can be done by the following code.

  1. fun main(args: Array<String>) {  
  2.     // In Java Object Created by ClassObject obj = new ClassObject();  
  3.     var model = Model()  
  4.     model.name = "Androidmads"   
  5. }  
Constructors

The Following code shows how to create and call constructors in Kotlin. Create a Model class with the constructor as shown in below.

  1. class Model{  
  2.    constructor(name:String, age:Int) {  
  3.   
  4.    }  
  5. }  

It can called as below.

  1. fun main(args: Array<String>) {  
  2.     // In Java Object Created by ClassObject obj = new ClassObject("Androidmads",20);  
  3.     var model = Model("Androidmads",20)  
  4.     println("${model.name}")  
  5. }  
Null Value Handling

In Java, Null value may lead to Null Pointer Exception. But, in Kotlin we have a Null Handling method to avoid this null pointer exception.

The Null Handling is done by using Nullable Values like in C#. For Example, Nullable String is denoted by String? The following code shows the usage Nullable values in Kotlin.

  1. fun main(args: Array<String>) {  
  2.     var name: String? = null  
  3.     // Just prints Null and does not throw NullPointerException    
  4.     println("${name}")