Skip to content

Instantly share code, notes, and snippets.

@jagad89
Last active February 10, 2020 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagad89/3362624b86ecc309f240d81e22915bc9 to your computer and use it in GitHub Desktop.
Save jagad89/3362624b86ecc309f240d81e22915bc9 to your computer and use it in GitHub Desktop.
Kotlin learning notes

My Kotlin notes

Typical hello world program with kotlin

fun main(){
    // Print Hello Kotlin
    println("Hello Kotlin")
    // it's time to say, "GOOD BYE SEMICOLON(;)"

    /* TOPIC: Variable */
    // prototype:
    // (var|val) variableName : <datatype> [=value]

    // variable declared with *val* keyword can not be reassign.
    val varibaleWithValDeclaration: String = "Hello Val, You can't reassign other value to this variable again"
    // uncommenting below line cause ERROR!!! because the variable declared with *val* keyword
    // varibaleWithValDeclaration = "Val reassignment error"

    // variable declared with *var* keyword can be reassign.
    var varibaleWithVarDeclaration: String = "Hello Var, You can reassign this variable as many times as you want"
    varibaleWithVarDeclaration = "var variable reassigned"
    println(varibaleWithVarDeclaration)

    /* TOPIC: Variable END */

    /* TOPIC: Nullable */
    // By default all datatype are not nullable in kotlin.
    // Bellow line will show error.
    // var nullableString: String = null

    // to make it nullable add ? sign after datatype
    var nullableString: String? = null
    /* TOPIC: Nullable END */

    /* TOPIC: if..else */
    val age = 14
    if(age >= 18){
        println("You can vote in elections!!")
    }else{
        println("You will be allowed to vote when you will 18 !!")
    }
    /* TOPIC: if..else END */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment