You are on page 1of 20

 

Kotlin - JVM language, Statically Typed, Object oriented, Functional 
 
DataTypes - Var and val are used to declare variable in Kotlin 
 

fun main(args: Array<String>) { 

    // In Kotlin, we don’t require usage of class explicitly to make our application run

    val myNumber = 10       // Int   -> explicit declaration of Int not required for Initialization
    var myDecimal = 1.0     // Float
    var isActive = true        // Boolean

    var myString: String    // Mutable String  & for declaration explicit type need to mention
    myString = "Hello World"
    myString = "Another World"    // var <-> mutable,   val <-> Immutable
 
    val myAnotherString = "My constant string value"    // Immutable String
 //  myAnotherString = "some value"  // NOT ALLOWED, since val is immutable

    print(myNumber)

 
 
To Pass a parameter to another function present inside class 

fun main(args: Array<String>) {

    val name = "Vivek"
    var personObj = Person()
    personObj.display(name)
}
// Parameters inside method ->  fun methodName (variableName:   dataType)
class Person{
    fun display(name: String)
    {
        print(name)
    }

 
 
 
 
 

 
 

String Interpolation 

fun main(args: Array<String>) {

    var name = "vivek"
    println("The name of the student is " + name)
    println("The name of the student is $name")

    var str = "hello " + name
    var str1 = "hello $name"    // we can use $ sign instead of + 
    println(str)                // hello vivek
    println(str1)             // hello vivek

    println("Str name is $str and length is ${str.length}")   //Str name is hello vivek and length is 11

    val a = 10
    val b = 5
    println("The sum of $a and $b is ${a + b}")  // The sum of 10 and 5 is 15

 
Double Dot Operators : Ranges used in loops 
 

fun main(args: Array<String>) {

    var r1 = 1..5                               //  The range contains the number 1,2,3,4,5
                                                        //   var r2 = 5..1 -> doesn't have like this for decreasing use downTo
    var r2 = 5 downTo 1                 //   The range contains the number 5,4,3,2,1

    var r3 = 5 downTo 1 step 2     //  The range contains 5,3,1 - two values has difference of 2

    var r4 = 'a'..'z'                            // The range contains values "a" , "b" , "c" .... "Z"

    var isPresent = 'c' in r4
    println(isPresent)                     //  prints true

    var r5 = 1.rangeTo(10)

    var r6 = 10.downTo(1)               // instead of double dot I used downTo and rangeTO

    var r6 = 10.downTo(1) step 3   // The range contains 10,7,4,1

 
 

IF as Expression 

fun main(args: Array<String>) {

    var a = 2
    var b = 5
    var maxValue: Int
    if (a > b)
        maxValue = a
    else
        maxValue = b
    println(maxValue)                  //  prints 5

    var max: Int = if (a > b)         //  In kotlin we can directly assign the value from If
        a
    else
        b
    println(max)                           //   prints 5

 
 
When as Expression 

fun main(args: Array<String>) {

    var x = 3
    when (x)                                             // in c and Java we have switch but in Kotlin we use when
    {
        1 -> println("x is 1")                      //  ( case 1: ) in java in kotlin ( 1 -> )
        2 -> println("x is 2")
        in 3..20 -> println("x in between 3 and 20")    // ranges can also be used
        else -> {
            println("x is unknown")           //  If we have multiple statements put that in block
            println("x is $x")                       //  Instead of default we use else
        }
    }

 
 
 
 
 
 
 

 
 

For Loop 

fun main(args: Array<String>) {

    var a = 1..10
    for(i in a)
        println(i)        // prints 1 to 10

    for(i in a step 2)
        println(i)       // prints 1,3,5,7,9 because we used step 2 so increment by 2

fun main(args: Array<String>) {

    myLoop@ for (i in 1..3)                             //break the loop using @
        for (j in 1..3) {
            println("$i  $j")
            if (i == 2 && j == 2)
                break@myLoop;
        }

 
Function as Expression 
 

fun main(args: Array<String>) {

    var larger = greater(3, 5)
    println("greater of two $larger")
}

fun greater(a: Int, b: Int): Int = if (a > b) {
    a
} else {
    b

 
 
 
 
 
 

 
 

Interoperability - Accessing Kotlin files in java and Java files in Kotlin  
 
MyFirst.kt 

fun main(args: Array<String>) {

    println("the area is ${MyJava.area(7, 5)}")    // accessing Java function
}

fun greater(a: Int, b: Int): Int {
    return if (a > b)
        a
    else
        b

 
MyJava.java 
 

public class MyJava {
    public static void main(String[] args) {

        int grt = MyFirstKt.greater(10, 7);             // accessing kotlin function
        System.out.println("The greatest element is " + grt);
    }

    public static int area(int a, int b) {
        return a * b;
    }

 
Named Parameters : Pure Kotlin Feature not present in java 

fun main(args: Array<String>) {
    volume(b = 20, c = 10, a = 30)
}
fun volume(a: Int, b: Int, c: Int = 40) {
    println("a = $a")   // a =30
    println("b = $b")   // b =20
    println("c = $c")   // c= 10

// No matter the sequence of parameters inside main if we assign to actual arguments name 

 
 

Extension Function -  
 

fun main(args: Array<String>) {

    var Student = Student()
    println("Pass Status : " + Student.isPassed(67))
    println("Scholar Status : " + Student.isScholar(67))
}

fun Student.isScholar(marks: Int): Boolean     // Extension function of class student 

                                            // This function will behave like a function present inside the class student  
{
    return marks > 95
}

class Student {                 //our Own Class

    fun isPassed(marks: Int): Boolean {
        return marks > 40;
    }

 
Primary constructor with initializer block 

class myClass(name: String, id: Int) {     // primary constructor does not have any code
    val e_name: String
    var e_id: Int
    init{                                                            // init block used to initialize the code 

        e_name = name
        e_id = id
        println("Name = ${e_name}")
        println("Id = ${e_id}")
    }
}
fun main(args: Array<String>){
    val myclass = myClass ("Ashu", 101)
}

//class myClass(name: String,  id: Int)  // the name and id can’t be used inside class they just need to used 
for assigning to another field variable inside init block (they are not properties of class)
/class myClass(var name: String , val id: Int) // the name and id can be used anywhere in the class 

 
 

Kotlin Secondary Constructor 

class MyClass {

    constructor(name: String, id: Int) {
        println("Name = ${name}")
        println("Id = ${id}")
    }
}

fun main(args: Array<String>) {
    val myclass = MyClass("Ashu", 101, 2)

// var and val is not possible in Secondary constructor these parameters can’t be modified and can’t  be used 
anywhere  

 
Calling one secondary constructor from another secondary constructor of same class 
 

class MyClass {

    constructor(name: String, id: Int) : this(name, id, "mypassword") {
        println("this executes next")
        println("Name = ${name}")
        println("Id = ${id}")
    }

    constructor(name: String, id: Int, pass: String) {

       // this executes first even calling above constructor because of using this() in above constructor

        println("this executes first")
        println("Name = ${name}")
        println("Id = ${id}")
        println("Password = ${pass}")
    }
}

fun main(args: Array<String>) {
    val myclass = MyClass("Ashu", 101)

 
 

Visibility Modifiers – public, private, internal, protected  

open class Base() {
    var a = 1                                                      // public by default  
    private var b = 2                                        //  private to Base class  
    protected open val c = 3                         //  visible to the Base and the Derived class  
    internal val d = 4                                      //   visible inside the same module  
    protected fun e() {}                                  //   visible to the Base and the Derived class  
}

class Derived : Base() {
    // a, c, d, and e() of the Base class are visible  
    // b is not visible  
    override val c = 9           // c is protected  
}

fun main(args: Array<String>) {
    val base = Base()
    // base.a and base.d are visible  
    // base.b, base.c and base.e() are not visible  
    val derived = Derived()
    // derived.c is not visible  
}   

 
Inheritance:  By default Classes are  : public & final , For inheritance you need to make class open 

open class Parent {
    fun fly() {
        println("flying...")
    }
}

class Child : Parent() {
    fun swim() {
        println("swimming...")
    }
}

fun main(args: Array<String>) {
    val duck = Child()
    duck.fly()                            // flying..
    duck.swim()                     // swimming..

 
 

Inheritance and primary Constructor 

open class Employee(name: String, salary: Float) {
    init {
        println("Name is $name.")
        println("Salary is $salary")
    }
}

class Programmer(name: String, dept: String, salary: Float) : Employee(name, salary) {
    init {
        println("Name $name of department $dept with salary $salary.")
    }

    fun doProgram() {
        println("Programming is my passion.")

    }
}

fun main(args: Array<String>) {
    val obj1 = Programmer("Ashu", "Development", 40000f)
    obj1.doProgram()
}  

 
Inheritance and Secondary Constructor 

open class Parent {

    constructor(name: String, id: Int) {
        println("execute super constructor $name: $id")
    }
}

class Child : Parent {

    constructor(name: String, id: Int, dept: String) : super(name, id) {
        print("execute child class constructor with property $name, $id, $dept")
    }
}

fun main(args: Array<String>) {
    val child = Child("Ashu", 101, "Developer")

 
 

Method Overriding

open class Bird {
    open var colour = "Black"
    open fun fly() {
        println("Bird is flying...")
    }
}

class Parrot : Bird() {
    override var colour = "green"
    override fun fly() {
        super.fly()                               // calling superclass method
        println("Parrot is flying...")
    }
}

fun main(args: Array<String>) {
    val p = Parrot()
    p.fly()                                         // Method overriding   - Parrot is flying...
                                                       //  we have used super in fly() method in derived it prints Bird is f
    println(p.colour)                      //  Property overriding - green

 
 
Abstract Class -All the variables (properties) and member functions of an abstract class are by default non-
abstract. So, if we want to override these members in the child class then we need to use open keyword. 
 

    //abstract class
abstract class Employee(val name: String, val experience: Int) {   // Non-Abstract
    // Property
    // Abstract Property (Must be overridden by Subclasses) property can’t be declared
    abstract var salary: Double

    // Abstract Methods (Must be implemented by Subclasses) method can’t have body
    abstract fun dateOfBirth(date: String)

    // Non-Abstract Method
    fun employeeDetails() {
        println("Name of the employee: $name")
        println("Experience in years: $experience")
        println("Annual Salary: $salary")
    }

 
 

// derived class
class Engineer(name: String, experience: Int) : Employee(name, experience) {
    override var salary = 500000.00
    override fun dateOfBirth(date: String) {
        println("Date of Birth is: $date")
    }
}

fun main(args: Array<String>) {
    val eng = Engineer("Praveen", 2)
    eng.employeeDetails()
    eng.dateOfBirth("02 December 1994")

 
 
Interface Properties and Methods in interface are abstract by default unlike abstract class (Not abstract by 
default), Normal methods are public and open by default but NOT FINAL unlike abstract class (final and public 
not open) , supports functionality of multiple inheritance. 
 

fun main(args: Array<String>) {
    var myButton = MyButton()
    myButton.onTouch()
    myButton.onClick()
}

interface MyInterfaceListener {        // You cannot create the instance of interface
    fun onTouch()                                 //  Methods in interface are abstract by default
    fun onClick() {                                 //  Normal methods are public and open by default but NOT FINAL
        println("MyInterfaceListener: onClick")
    }
}

interface MySecondInterface {       // You cannot create the instance of interface
    fun onTouch() {                             // Normal Method
        println("MySecondInterface: onTouch")
    }

    fun onClick() {                 // Normal methods are public and open by default but NOT FINAL
        println("MySecondInterface: onClick")

 
 

    }
}

class MyButton : MyInterfaceListener, MySecondInterface {
    override fun onTouch() {
        super<MyInterfaceListener>.onClick()               // same method in both interface use super keyword 
        super<MySecondInterface>.onClick()
    }

    override fun onClick() {
        super.onTouch()
    }

 
 
Tailrec Functions - recursion which performs the calculation first, then makes the recursive call 
 
 

fun main(args: Array<String>) {
    var number = 100000000L
    var dx = recursivesum(number)
    println(dx)

tailrec fun recursivesum(n: Long, semiresult: Long = 0): Long {
    return if (n <= 0) {
        semiresult;
    } else {
        recursivesum(n - 1, n + semiresult)
    }

 
 
 
 
 
 
 
Lambda Expressions 
 
val lambda_name : Data_type = { argument_List -> code_body } 
 

 
 

val sum1 = { a: Int, b: Int -> a + b }

// without type annotation in lambda expression
val sum2: (Int, Int) -> Int = { a, b -> a + b }
fun main(args: Array<String>) {
    val result1 = sum1(2, 3)
    val result2 = sum2(3, 4)
    println("The sum of two numbers is: $result1")
    println("The sum of two numbers is: $result2")

    // directly print the return value of lambda without storing in a variable.
    println(sum1(5, 7))

val sum1 = { a: Int, b: Int ->
    val num = a + b
    num.toString()     //convert Integer to String
}

fun main(args: Array<String>) {
    val result1 = sum1(2, 3)
    println("The sum of two numbers is: $result1")

Type declaration in lambdas – 

We must explicitly declare the type of our lambda expression. If lambda returns no value then we can 
use: Unit 
Pattern: (Input) -> Output 
Lambdas examples with return type –   
● val lambda1: (Int) -> Int = (a -> a * a) 
● val lambda2: (String, String) -> String = { a , b -> a + b } 
● val lambda3: (Int)-> Unit = {print(Int)} 
 
Higher Order Functions - function which can accepts a function as parameter or can returns a function 
 
There are two types of lambda expression which can be passed-  
● Lambda expression which return Unit   -. lmbd: () -> Unit 
● Lambda expression which return any of the value integer string->.lmbd: (Int, Int) -> Int 
 

 
 

// with type annotation in lambda expression
// lambda expressions returns unit and below one returns int
var lambda = { println("GeeksforGeeks: A Computer Science portal for Geeks") }

var lambda1 = { a: Int, b: Int -> a + b }

// higher-order function
fun higherfunc(lmbd: () -> Unit) {     //  accepting lambda as parameter
    lmbd()                                              //   invokes lambda expression
}

fun higherfunc1(lmbd: (Int, Int) -> Int) {     // accepting lambda as parameter

    var result = lmbd(2, 4)                //   invokes the lambda expression by passing parameters 
    println("The sum of two numbers is: $result")
}

fun main(args: Array<String>) {
                                                       //  invoke higher-order function
    higherfunc(lambda)               //   passing lambda as parameter
    higherfunc1(lambda1)           //  passing lambda1 as parameter

 
Anonymous Functions-   similar to a regular function except for the name of the function which is omitted 
from the declaration. The body of the anonymous function can be either an expression or block. 
 

// anonymous function with body as an expression
val anonymous1 = fun(x: Int, y: Int): Int = x + y

// anonymous function with body as a block
val anonymous2 = fun(a: Int, b: Int): Int {
    val mul = a * b
    return mul
}

fun main(args: Array<String>) {   //invoking functions
    val sum = anonymous1(3, 5)
    val mul = anonymous2(3, 5)
    println("The sum of two numbers is: $sum")
    println("The multiply of two numbers is: $mul") 

 
 

 
Collections - store group of related objects (List , Set, Map) 
 
Collections are two types:  
●  Immutable  -  listOf() , mapOf(), setOf() 
●  Mutable      -  arrayListOf(), mutableListOf(), hashMapOf(), mutableMapOf(), hashSetOf(), 
mutablesetOf() 
 
Listof() 

fun main(args: Array<String>) {                          // variable name followed by : follows dataType 

    var stringList: List<String> = listOf<String>("Ajay", "Vijay", "Prakash", "Vijay", "Rohan") 

                                                                                  // var and val are used to declare variable

    var intList = listOf<Int>(24, 3, 78, 64)               // list is immutable we can't increase or decrease size
    for (index in intList) {                                          // $ sign to get direct value instead of concatenation
        print("$index,")                                                 // prints 24,3,78,64
    }

    println()
    println(stringList.get(0))                                    // Ajay
    println(stringList.indexOf("Vijay"))                   // 1 -> first Index
    println(stringList.lastIndexOf("Vijay"))            // 3 ->  last Index having vijay
    println(stringList.size)                                       //  5 -> size of list
    println(stringList.contains("Prakash"))           // if contains return true
    println(stringList.subList(2, 4))                        // [Prakash, Vijay] -> from index 2 to 4 less 1
    println(stringList.isEmpty())                             // false -> if empty return true
    println(stringList.drop(1))                                 // prints elements in List except last 1
    println(stringList.dropLast(2))                          // prints elements in List except last 2
    println(stringList)

    val sortlist = intList.sorted();                             // list is mutable we can't apply sorting to list itself
    println(sortlist);                                                   // [3,24,64,78]

 
 
 
 
 
 
 
 
 

 
 

ArrayListOf() 

import java.util.*
import kotlin.collections.ArrayList

fun main(args: Array<String>) {

    var read = Scanner (System.`in`)                    // to read input from user
    var arrayList = ArrayList<String>()                 //Creating an empty arraylist
    arrayList.add("Ajay")                                        //Adding object in arraylist
    arrayList.add("Vijay")                                       // arrayList is mutable we can increase or decrease size 
    arrayList.add("Prakash")
    arrayList.add("Rohan")                                   // add elements to list using .add
    arrayList.add("Vijay")
    println(".......print ArrayList.......")
    for (i in arrayList) {
        println(i)                                                       // prints elements in arrayList
    }

    println("Enter elements into intList:")
    var intList = ArrayList<Int>()
    for (i in 0..5) {
        intList.add(read.nextInt())              // reading elements from console
    }

    println(arrayList.size)                                   // 5
    println(arrayList.get(2))                                // prakash prints index of 2 element in list
    arrayList.set(3, "Ashu")                                // Mutable array we can modify elements
    println(arrayList[3])                                       // prints Ashu
    println(arrayList.indexOf("Vijay"))              //1 ->  first occurence Index having vijay
    println(arrayList.lastIndexOf("Vijay"))       //4-  >  last Index having vijay
    arrayList.remove("Vijay")                            // removes vijay of first occurence
    // arrayList.removeAt(index)
    println(arrayList)                                          // [Ajay, Prakash, Ashu, Vijay] after removing vijay
    arrayList.clear()                                           // to clear all elements in arraylist
    println(arrayList)                                         // [ empty array ] after clearing all elements

    println(intList)                                            //   [5, 9, 1, 3, 4, 7]
    val a = intList.sortedDescending()          //   sorted in descending order
    println(a)                                                     //   [9, 7, 5, 4, 3, 1]  - > sort in descending order
    val itr = intList.iterator()                           // using iterator for printing elements in list
    while (itr.hasNext()) {
        println(itr.next())
    }

 
 

MapOf() - key and value pair. Map keys are unique and hold only one value for each key. The key and value may 
be of different pairs such as <Int, Int>,<Int, String>, <Char, String>etc. This interface is immutable, fixed size and 
its methods support read only access. 
 

fun main(args: Array<String>) {

    val myMap = mapOf<Int, String>(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")
    for (key in myMap.keys) {
        println(myMap[key])
    }

    println(myMap)                                                   //   {1=Ajay, 4=Vijay, 3=Prakash}
    println(myMap.keys)                                         //    [1, 4, 3]  -> keys in Map
    println(myMap.values)                                      //    [Ajay, Vijay, Prakash]   -> value sin map
    println(myMap.size)                                          //     3 -> size of map
    println(myMap.containsKey(3))                      //     true ->  key value of 3 is present
    println(myMap.containsValue("Ajay"))          //     true ->  value of ajay present
    println(myMap.get(4))                                      //     vijay ->  key of 4 present and it prints that value
    println(myMap.get(12))                                    //     null -> key of 12 not present
     

   myMap.plus(Pair(5, "Rohan"));  
    println(myMap)                                           // {1=Ajay, 4=Vijay, 3=Prakash} mutable we can't add to myMap

    var list = myMap.plus(5 to "vijay")
    println(list)                                                    // {1=Ajay, 4=Vijay, 3=Prakash, 5=vijay} we can add to other var
   

   // two values with same key
    val map = mapOf(1 to "Vivek", 2 to "for", 1 to "Vivek")
    //If two values have same key value , then the map will contain the last value of the those numbers.
    println("Entries of map : " + map.entries)     //Entries of map : [1=vivek, 2=for]

 
 
 
 
 
 
 
 
 
 
 

 
 

HashMapOf() 

import java.util.*
import kotlin.collections.HashMap

fun main(args: Array<String>) {

    var read = Scanner(System.`in`)
    val stringMap: HashMap<String, String> = hashMapOf<String, String>("name" to "Ashu")
    stringMap.put("city", "Delhi")
    stringMap.put("department", "Development")
    stringMap.put("hobby", "Playing")

    val intMap: HashMap<Int, String> = hashMapOf<Int, String>(1 to "Ashu", 2 to "Ajeet")

    println("Enter key and value")
    for (i in 0..2) {
        var key: Int = read.nextInt()
        var value: String = read.next()             //adding <key, value> pair from input by user 
        intMap.put(key, value)
    }
    println("..... intMap........")
    for (key in intMap.keys) {
        println(intMap[key])
    }
    println(stringMap.containsKey("name"))      // true -> key of "name" present in stringMap
    println(stringMap.containsValue("Delhi"))   // true -> value of "Delhi" present in stringMap

    println(stringMap)                              // {name=Ashu, department=Development, city=Delhi, hobby=Playing}
    stringMap.replace("city", "Mumbai")         // replaces value of key(if present)with new one
    println(stringMap)                          // {name=Ashu, department=Development, city=Mumbai, hobby=Playing}

    println(stringMap.size)                     // 4 -> size of Map

    stringMap.remove("city")                   //   remove <key,value> pair of key(city) from Map
    println(stringMap)                               //   {name=Ashu, department=Development, hobby=Playing}

    stringMap.set("name", "vivek")         //  {name=vivek, department=Development, hobby=Playing}
    println(stringMap)                              //  set value for key(if present)

    stringMap.set("skill", "Kotlin")           //  add <key, value> if key(not present)
    println(stringMap)                              // {name=vivek, department=Development, hobby=Playing, skill=Kotlin}

 
 

Setof() -unordered collection of elements, does not support duplicate elements. 
 

fun main(args: Array<String>) {
    var intSet = setOf(2, 6, 4, 29, 4, 5)
    var mySet: Set<Any> = setOf(2, 6, 4, 29, 4, 5, "Ashu", "Ajay")
                                                                         //set<Any> contains value of any datatype

    println(".......print Any set.........")
    for (element in mySet) {
        println(element)                                      // prints elements in mySet
    } 

    println(mySet.contains("Ashu"))             // true -> set contains Ashu return true
    println(mySet.containsAll(intSet))          // true  -> if all elements of intSet present in any set
    println(mySet.isEmpty())                         // false -> if set is empty

    val remainList = mySet.drop(4)             // drops first 4 elements from set and copy all other to remainList
    println(remainList)                                  // [5, Ashu, Ajay]

    println(mySet.elementAt(3))                // 29  ->  prints element at particular index

    println(mySet.first())                             // 2 -> prints first element in set
    println(mySet.last())                             //Ajay -> prints last element in set

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 
 

HashSetOf() 
 

import java.util.*
import kotlin.collections.HashSet

fun main(args: Array<String>) {

    var read = Scanner(System.`in`)
    var hashSet = HashSet<Int>()

    println("Enter elements")
    for (i in 0..2) {
        hashSet.add(read.nextInt())      // reading input from user
    }

    println("......hashSet......")
    for (element in hashSet) {
        println(element)                                  
    }

    val intSet = setOf(6, 4, 29)
    hashSet.addAll(intSet)
    println(hashSet)                                           //     [1, 2, 4, 6, 8, 29]  -> after adding intSet

    println(hashSet.size)                                   //    6 -> size of hashset
    println(hashSet.contains(13))                   //     false  ->  if 6 presents in hashSet return true
    println(hashSet.containsAll(intSet))        //  true   -> if all elements in intSet present in hashSet returns true

    hashSet.remove(2)
    hashSet.removeAll(intSet)
    println(hashSet)                                          // [1, 8] - > after removing intList and (remove(2))

    println(hashSet.isEmpty())                        // false  ->   returns true if hashSet is Empty

You might also like