Kotlin For Android App
Learn to teach
I’m going to write kotlin cheats that i have learned from Kotlin docs…
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);
}
Strings and variable println together
→ println(“value of a = $a and b = $b”)
\=> here with some operations
→ println(“sum = ${a+b}”)
function defined as fun funName(){}
fun addNum(){
println(“add fun called”)
}
function type in Kotlin
return fun can be written as →
fun sum(a:Int , b: Int):Int = a+b
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
}
without creating getter and setter, U can get and set value in Kotlin class because kotlin provides these……
- class Student{
var name: String = ““
var age: Int = 0
isAdmitted(){
// TODO
}
// setter
set(value){
field = value
println(“$value setted”)
}
// getter
get(){return field}
}
Use lateinit means u will initialize later to avoid compile time error
eg : → lateinit favouriteMovie: String
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
}
}
Inheritance in Kotlin[parent class should be open] →
open class Vehicle(){}
class Car(): Vehicle(){}
use data in class for compare string, return string and many override funs benefit
class type : → class, open class, abstract class,enum, inteface
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") } }Deligation for inherit two or more class once →
class App: A by FirstDelegate(),B by SecondDelegate(){
// override funs
}
Interface stuff →
interface A{}
interface B{}
open class FirstDelegate : A{}
open class SeciondDelgate : B{}
Loops in Kotlin
- 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
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)
}
when condition →
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") }


