Skip to main content

Command Palette

Search for a command to run...

Kotlin For Android App

Published
3 min read
B

Learn to teach

I’m going to write kotlin cheats that i have learned from Kotlin docs…

  1. All variables are in kotlin defined as var (Immutable/not changeable) and val(muttable/changeble)..

    And there is no need of semicolon

// u can define the type of the or can’t

val name: String = “AB” || val name = “AB”

fun main(){
    val doubleNumber: Double = 2.5
    val floatNumber: Float = 2.5F
    val floatNumber2 = 28F
    println(floatNumber2)
    println(doubleNumber)
    println(floatNumber)
    val charValue: Char = 'A'
    println(charValue)
    val booleanValue = true;
    println(booleanValue);
}
  1. Strings and variable println together

    → println(“value of a = $a and b = $b”)

    \=> here with some operations

    → println(“sum = ${a+b}”)

  2. function defined as fun funName(){}

    fun addNum(){

    println(“add fun called”)

    }

  3. function type in Kotlin

    1. return fun can be written as →

    2. fun sum(a:Int , b: Int):Int = a+b

    3. many args fun is here and there are many types of function u can learn from docs….

    fun main(){
        println("Sum is = ${addNumbers(28,10,25)}")
    //    unknown numbers sum
        println("Unknow numbers sum = ${addManyNumbers(28,25,123,123,45)}")
    }
    //know how many numbers will be
    fun addNumbers( a: Int,b: Int,c: Int): Int{
        return a+b+c
    }

    //unknown how many numbers will be
    //kotlin treats vararg as array  [Array<Type>]
    fun addManyNumbers(vararg numbers: Int ): Int{
        var sum = 0
        for (i in numbers){
            sum += i
        }
        return sum
    }
  1. without creating getter and setter, U can get and set value in Kotlin class because kotlin provides these……

    1. class Student{

var name: String = ““

var age: Int = 0

isAdmitted(){

// TODO

}

// setter

set(value){

field = value

println(“$value setted”)

}

// getter

get(){return field}

}

  1. Use lateinit means u will initialize later to avoid compile time error

    eg : → lateinit favouriteMovie: String

  2. In class companion object Kotlin means static keyword in Java,[ so u can access by class name without creating an object]

    eg: → class Calculator{

    companion object{

    fun sum(a: Int, b: Int): Int = a+b

    }

    }

  3. Inheritance in Kotlin[parent class should be open] →

    open class Vehicle(){}

    class Car(): Vehicle(){}

  4. use data in class for compare string, return string and many override funs benefit

  5. class type : → class, open class, abstract class,enum, inteface

  6. enum class Direction(var direction: String,var distance: Int){
        NORTH("north",10),
        SOUTH("south",20),
        EAST("east",20),
        WEST("west",30);
        fun getData(){
            println("Direction = $direction and distance = $distance")
        }
    }
    
  7. Deligation for inherit two or more class once →

    class App: A by FirstDelegate(),B by SecondDelegate(){

    // override funs

    }

  8. Interface stuff →

    interface A{}

    interface B{}

    open class FirstDelegate : A{}

    open class SeciondDelgate : B{}

Loops in Kotlin

  1. For loop exlude those are in Java
fun main(){
//    all include
    for(i in 1..10){
        print(" $i")
    }
    println()
//    until means not include
    for(i in 1 until 10)print(" $i")
    println()
//    downTo means decrement order
    for (i in 10 downTo 1)print(" $i")
    println()
//    steps jump in loop
    for (i in 1..10 step 2) print(" $i")

    println()
//    if statement as checking
    for(i in 1..10){
//        check numbers
        if(i in 4..9)continue

        println(i)
    }
}

Conditions exlude those are in Java

  1. if condition [u can return from if(){}else{}]

    a. if(true){}

    b. if(i in 1..10){} // check i is in between 1 - 10

fun main(){
    if(true){
        println("True statement")
    }else{
        println("False statement")
    }

    val ifValue = if(false)"False" else "True"
    println(ifValue)

}
  1. when condition →

  2.  val direction = Direction.valueOf("south".uppercase())
    
     when(direction){
         Direction.EAST -> println("Direction is East")
         Direction.WEST -> println("Direction is West")
         Direction.NORTH -> println("Direction is North")
         Direction.SOUTH -> println("Direction is South")
     }