Showing posts with label Kotlin Basics. Show all posts

Kotlin - Basics


Hello Guys, we already knew that Google announced Kotlin is a new first class language for Android Development. So, we started the Kotlin series for learning Kotlin for android. We have seen the "Hello World" Program for Android in our previous post. 

You can download Intellij idea Commuinty Edition or use Kotlin Online Compiler provided by Intellij

In this post, we will learn the basics of kotlin.

1. Hello World

The Following Snippet shows the Hello world Program and is similar to Java.
fun main(args: Array <string>) {
    println("Hello, world!")
}

2. Variables and Constants

The Following Snippet shows the How to use Variables and Constants with Datatype in Kotlin.
  1. Variables can be re-assigned 
  2. // Variable - Values Can be reassigned
    var a = 10
    a = a + 10
    println(a)
  3. Constants cannot be re-assigned and if we did will error as "val cannot be reassigned"
  4. // Constants - Values cannot reassigned
    val x = 10
    println(x)
  5. We can specify the Data types for Variables and Constants
  6. // Concate String Values
    println(str+"Mad")
    println("${str}Mad - Kotlin Series")
The following snippet shows how to use variables and constants.
fun main(args: Array<string>) {
    // Variable - Values Can be reassigned
    var a = 10
    a = a + 10
    println(a)
    // Constants - Values cannot reassigned
    val x = 10
    println(x)
    // Variables - with Datatype
    var str : String = "Android"
    println(str)
    // Concate String Values
    println(str+"Mad")
    println("${str}Mad - Kotlin Series")
}

3. If Else

The Following Snippet shows the If Else Statement in Kotlin.
fun main(args: Array<string>) {
    var arun = 10
    var dharun = 15
    var elder = if(arun > dharun)"Arun" else "Dharun"
 
    println("Elder is ${elder}")
}

4. When

Switch statement is not present in Kotlin and is replaced by When.
fun main(args: Array<string>) {
    var elder = "Dharun"
    when(elder){
        "Arun"->{
            println("Elder is Arun")
        }
        "Dharun"->{
            println("Elder is Dharun")
        }
        else->{
            println("No Answer")
        }
    }
}

5. Loops

The Following snippet shows Loops in Kotlin.
// For Loop
fun main(args: Array<string>) {
    for(i in 1..10){
        println(i)
    }
}
// While Loop
fun main(args: Array<string>) {
    var x = 1;
    while(x<=10){
        println(x)
        x++
    }
}

6. Arrays and Lists

The Following snippet shows Arrays in Kotlin.
fun main(args: Array<String>) {
    var x = arrayOf(1,2,3,4);
    for(i in x){
        println(i)
    }
}
We can apply any type in like float, string ...
fun main(args: Array) {
    var x = arrayOf(1,2,3,4,"Androidmads");
    for(i in x){
        println(i)
    }
}
It Can be controlled by apply Data types as in the following Snippet
fun main(args: Array<String>) {
    var x = arrayOf<String>("Android","Mads","Kotlin","Series");
    for(i in x){
        println(i)
    }
}
Lists are similar to arrays
fun main(args: Array<String>) {
    var x = listOf<String>("Android","Mads","Kotlin","Series");
    for(i in x){
        println(i)
    }
}

7. Classes

The Following snippet shows how to declare / create objects for class in Kotlin. Create a class named it as "Model.kt"
class Model {
    var name : String = ""
}
fun main(args: Array<String>) {
    // In Java Object Created by ClassObject obj = new ClassObject();
    var model = Model()
    model.name = "Androidmads" 
}

8. Constructors

The Following snippet shows how to use constructor in Kotlin.
class Model(val name:String, val age:Int)
We can also the following method
class Model{
   constructor(name:String, age:Int) {

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

9. Null Handling

In Java, Null value may leads to Null Pointer Exception. But, In Kotlin,Null Handling to variables is done as in the following snippet
fun main(args: Array<String>) {
    var name:String? = null
    // Just prints Null and does not throw NullPointerException  
    println("${name}")
}
Hope you Like this. Post Comments about this article in Comment Section