100%(3)3 out of 3 people found this document helpful
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 applicationsclient-side web applicationsandroid applicationsval and varKotlin 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 FlowKotlin 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.FunctionsKotlin 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 ObjectsThe 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()ConstructorKotlin 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
used to achieve constructor overloading.class Employee{constructor(name: String) {//initialization}}--------------In Kotlin-Ultimo Paso, you will learn:Lambda and Higher-Order functionsSingle-Expression functionsInfix notation functionsVarargs functionsGeneric functionsLambda FunctionsLambda 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 FunctionExample: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.