Kotlin Ultimo Salto - Handson.txt - Kotlin is a statically typed programming language for modern multiplatform applications Kotlin can be used to

Kotlin Ultimo Salto - Handson.txt - Kotlin is a statically...

This preview shows page 1 - 3 out of 23 pages.

Kotlin is a statically typed programming language for modern multiplatform applications. Kotlin can be used to develop: server-side applications client-side web applications android applications val and var Kotlin identifies the data type of a variable through a mechanism called type inference, and has two keywords for declaring variables: val: defines constants, and once assigned, values cannot change. var: defines variables whose value can change over time. Control Flow Kotlin has the following expressions: if..else and when for branching. for and while for looping. return to return a value from a function. break to break execution of the nested block. continue to skip current execution, and proceed to the next. Functions Kotlin functions are defined by using the keyword fun. Default arguments can be passed by using = while defining function parameters. fun default(len:Int = 5){ .... } Named Arguments : You can call a function with arguments by specifying argument names. default(len=11) Functions that return nothing can either have return type as Unit, or need not mention anything. fun printSomething():Unit{...} Extension functions allow you to add functionalities to existing classes. Classes and Objects The keyword class is used to declare classes. private,public, protected,internal are access modifiers. Objects can be created without using a new keyword. var emp = Employee(); Members are accessed by using the . keyword. emp.getName() Constructor Kotlin supports 2 types of constructors: Primary constructor: Specified along with the class header. class Employee(val name:String){ ... } Initializer block is used to initialize properties. class Employee(val name:String){ init{ //initialization } } Secondary Constructor is defined by using the keyword constructor,and can be
Image of page 1
used to achieve constructor overloading. class Employee{ constructor(name: String) { //initialization } } -------------- In Kotlin-Ultimo Paso, you will learn: Lambda and Higher-Order functions Single-Expression functions Infix notation functions Varargs functions Generic functions Lambda Functions Lambda functions are special functions which are not defined, but are passed immediately as expression. Syntax : val someName : Type = { arguments -> functionBody } Except functionBody, everything else is optional. The following lambda will calculate the square of a given number. val squareLambda = { number: Int -> number * number } println(squareLambda(5)) Lambda Function Example: fun main(args: Array<String>) { val arr = arrayOf(1,2) print(max(arr,{a,b -> a>b})) } fun max(nums:Array<Int>,filter: (a:Int,b:Int) -> Boolean):Int { if(filter(nums[0],nums[1])) return nums[0] return nums[1] } Behavior of the max function is controlled by the lambda function ({a,b -> a>b}), which is passed as an argument. The function max is a higher-order function. A function that consumes functions as parameters, or returns another function is called a higher-order function.
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 23 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture