Tutorials Point, posted on 2nd Nov, 2017

Inline Function

fun main(args: Array<String>) {
   val mylambda:(String)->Unit  = {s:String->print(s)}
   val v:String = "TutorialsPoint.com"
   myFun(v,mylambda) //passing lambda as a parameter of another function 
}

fun myFun(a :String, action: (String)->Unit) { //passing lambda 
   print("Heyyy!!!")
   action(a)// call to lambda function
}

Tutorials Point, posted on 2nd Nov, 2017

inheritance

import java.util.Arrays

open class ABC {
   open fun think () {
      print("Hey!! i am thinking ")
   }
}
class BCD: ABC() { // inheritance happens using default constructor 
   override fun think() {
      print("I Am from Child")
   }
}
fun main(args: Array<String>) {
   var  a = BCD()
   a.think()
}

Tutorials Point, posted on 2nd Nov, 2017

When

fun main(args: Array<String>) {
   val x:Int = 5
   when (x) {
      1,5 -> print(" Value of X either 1,5")
      
      else -> { // Note the block
         print("x is neither 1 nor 2")
      }
   }
}

Tutorials Point, posted on 2nd Nov, 2017

Kotlin Hello World!

fun main(args: Array<String>) { 
    println("Hello, World!") 
}

Tutorials Point, posted on 30th Oct, 2017

Exception Handling

fun main(args: Array<String>) { 
    try{ 
        val myVar:Int=12;        val v:String="Tutorialspoint.com" 
        v.toInt();
    }catch(e:Exception){ 
      e.printStackTrace();
    }finally{ 
        println("Exception Handeling in Kotlin");
    } 
}

Tutorials Point, posted on 30th Oct, 2017

Destructuring Declarations

fun main(args: Array<String>) { 
    val s=Student("TutorialsPoint.com","Kotlin") 
    val (name,subject)=s 
    println("You are learning "+subject+" from "+name) 
}  
data class Student( val a :String,val b: String ){ 
   var name:String=a 
   var subject:String=b 
}

Tutorials Point, posted on 30th Oct, 2017

Lambda Function

fun main(args: Array<String>) { 
    val mylambda :(String)->Unit ={s:String->print(s)} 
    val v:String="TutorialsPoint.com" 
    mylambda(v) 
} 

Tutorials Point, posted on 30th Oct, 2017

Function

fun main(args: Array<String>) { 
     
    println(MyFunction("tutorialsPoint.com")) 
} 
fun MyFunction(x: String): String { 
    var c:String ="Hey!! Welcome To ---" 
    return (c+x) 
}

Tutorials Point, posted on 30th Oct, 2017

Deligation

import kotlin.properties.Delegates  
class User { 
    var name: String by Delegates.observable("Welcome to Tutorialspoint.com") { 
        prop, old, new -> 
        println("$old -> $new") 
    } 
}  
fun main(args: Array<String>) { 
    val user = User() 
    user.name = "first" 
    user.name = "second" 
}

Tutorials Point, posted on 30th Oct, 2017

Using Lazy

val myVar: String by lazy { 
     
    "Hello" 
}  
fun main(args: Array<String>) { 
    println(myVar +" My dear friend") 
     
}

Advertisements
Loading...