Skip to content

Instantly share code, notes, and snippets.

@rahulkhatri19
Created February 18, 2020 04:50
Show Gist options
  • Save rahulkhatri19/56c03a666a6fd715688f09db1afaab29 to your computer and use it in GitHub Desktop.
Save rahulkhatri19/56c03a666a6fd715688f09db1afaab29 to your computer and use it in GitHub Desktop.
Kotlin Basic:
1. Null safe operator (?) :
println(name?.toUpperCase()) // if upper case then print name in uppercase else print null.
in java :
if (name.toUppercase != null){
print(name.toUpperCase())
} else print("null")
2. Not null assert(!!):
println(name!!.toUpperCase()) // if upper case then print name in upper case else null pointer exception.
fun main(args: Array<String>){
var name:String? = null
name = "rahul"
println(name.toUpperCase())
}
3. constructor :
class Student(val name:String, val rollNo:Int, var rank:Int){ }
or
class Student{
val name:String
val rollNo:Int
var rank:Int
// constructor by default name as val and it can't be changed and no need of annotation val as it by default.
construcutor(name:String, rollNo:Int, rank:Int){
this.name = name
this.rollNo = rollNo
this.rank = rank
}
}
// TO print any thing inside a constructor we need use init.
class Student(val name:String, val rollNo:Int, var rank:Int){
init {
println("constructor is called")
}
}
example :
4. secondary constructor:
class Student(val name:String, val rollNo:Int, var rank:Int){
construcutor(rollNo:Int, rank:Int) : this("", rollNo, rank){}
}
5. static method:
for making an menthod static we need to use companion object
class Studnet {
companion object{
fun getMethod(){}
}
}
class Teacher{
Student.getMethod()
}
6. if else:
simple as if(){} else{}
or like ?: operator // can use return keyword.
if (name != null) println(name.toUpperCase()) else println("null ops")
7. let function:
8. when exp : Similar to switch statement
val a = readLine()?.toInt()
when (a){
1, 3, 5 -> println("It is a odd les than 5")
2, 4, 6, 8 -> println("it is even less 10")
7 -> println(" It is 7")
else -> println("I dont know")
}
9. while loop:
var a = 2
while(a < 8) {
println("value of a=$a")
a++
}
10. for loop:
val userList = ArrayList<User>()
userList.add( User("1", "name 1", "img 1", "prof 1"))
userList.add( User("2", "name 2", "img 2", "prof 2"))
userList.add(User("3", "name 3", "img 3", "prof 3"))
userList.add(User("4", "name 4", "img 4", "prof 4"))
for (user:User in userList){
println("${user.id} name= ${user.name} image= ${user.imageUrl}")
}
loop with hash map
val userList = HashMap<Int, User>()
userList.put(1, User("1", "name 1", "img 1", "prof 1"))
userList.put(2, User("2", "name 2", "img 2", "prof 2"))
userList.put(3, User("3", "name 3", "img 3", "prof 3"))
userList.put(4, User("4", "name 4", "img 4", "prof 4"))
for ((key, value) in userList){
println("$key is ${value.id} name= ${value.name} image= ${value.imageUrl}")
}
Itrating loop with range operator (..)
for (i in 0..9){
println(i)
}
Range oprerator other use :
(0..9).forEach { i -> println(i) }
or
(0..9).forEach { it }
(9 downTo 0).forEach { println(it) } // print reverse 9 to 0
(0 until 9).forEach { println(it) } // print one digit less than 9
(0..7 step 2).forEach { println(it) } // print with two diff 0,2,4,6
('A'..'E').forEach { println(it) } // print A to E
val intArray:IntArray = intArrayOf(1,2,3,4,5,6)
val color = listOf("Red", "Blue", "Green")
intArray.set(2,-4)
intArray[3] = -7
intArray.forEach { println(it) } // 1 2 -4 -7 5 6
println(color[1]) // Blue
11. Exception Handling:
val result = try {
divide(5,23)
} catch (e: Exception){
println(e)
}
12. String to List
val cbList: MutableList<Int> = mutableListOf<Int>()
cbList.add(5)
cbList.add(8)
cbList.add(7)
val stcbList = cbList.toString()
Log.e("list",": $stcbList")
val listCb= stcbList.removeSurrounding("[","]").replace(" ","").split(",").map { it.toInt() }
Log.e("cb list", ": $listCb")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment