Friday 29 October 2021

Kotlin- When


NOTE :1) Whenever we have more then on condition that condition use "when" its a enhance version of switch condition


fun main(){

val userType = "admin"

when(userType){

"admin"->{

println("You'r admin")

}

"editor"->{

println("You'r editor")

}

"author"->{

println("You'r author")

}

else->{

println("You'r Other")

}

}

}


Output: You'r admin

Kotlin - Null Safty

 note : 1) Bydefault All variable non-null 

   2) For Accecpting this null value we have to put '?' with string. 

   3) If variable is null and want to access the properties of variable in that case use 'varible?.' 

   4) if confirm varible is not null then we can use '!!' but if not sure dont use it trow null pointer exception.


var name:String? = null

val Lname:String = "Nishad"

firstname

println("Hello${name?.length}")

Kotlin First Program 'Hello word'


  1. Kotlin code is usually defined in packages. Package specification is optional: If you don't specify a package in a source file, its content goes to the default package.
  2. An entry point to a Kotlin application is the main function. Since Kotlin 1.3, you can declare main without any parameters. The return type is not specified, which means that the function returns nothing.
  3. println writes a line to the standard output. It is imported implicitly. Also note that semicolons are optional.

Example:

val name:String="Kamal"

val Lname:String="Nishad"

fun main(){

println("Hello $name $Lname")

}


OUTPUT: Hello Kamal Nishad

Kotlin - Observer

 Rxjava -  Observer - Based on data view willl render (react) karega. trotling -> making stream for execution (Means clicks event fire kr...