type inference doesn’t always work in Kotlin. For instance, if we late initiate a variable, Kotlin cannot automatically infer the type of the variable, and we need to declare the
val a: Int = 10
in Kotlin the first letter of Int is upper case while in Java it is lower case. This change is actually quite significant
val
is used to declare an immutable
variable, and var is used to declare a mutable variable
fun methodName(param1: Int, param2: Int): Int {
return 0
}
import kotlin.math.max
fun largerNumber(num1: Int, num2: Int): Int {
return max(num1, num2)
}
fun largerNumber(num1: Int, num2: Int): Int = max(num1, num2)
largerNumber()
will also return an Int type. Thus, there is no need to explicitly define the type of the return data, and code can be further simplified to the following
fun largerNumber(num1: Int, num2: Int) = max(num1, num2)
fun largerNumber(num1: Int, num2: Int): Int {
var value = 0
if (num1 > num2) {
value = num1
} else {
value = num2
}
return value
}
val value = if (num1 > num2) {
num1
} else {
num2
}
fun largerNumber(num1: Int, num2: Int) = if (num1 > num2) {
num1
} else {
num2
}
fun largerNumber(num1: Int, num2: Int) = if (num1 > num2) num1 else num2
fun getScore(name: String) = when (name) {
"Tom" -> 86
"Jim" -> 77
"Jack" -> 95
"Lily" -> 100
else -> 0
}
fun checkNumber(num: Number) {
when (num) {
is Int -> println("number is Int")
is Double -> println("number is Double")
else -> println("number not support")
}
}
There is another way of using when statement that is not used very often but can be extensible in certain scenarios
notice that in Kotlin we can use ==
to determine if two strings or objects are equal
fun getScore(name: String) = when {
name.startsWith("Tom") -> 86
name == "Jim" -> 77
name == "Jack" -> 95
name == "Lily" -> 100
else -> 0
}
val range = 0..10
0 and 10 are all included in this rangefun main() {
for (i in 0..10) {
println(i)
}
}
val range = 0 until 10
//0-9fun main() {
for (i in 0 until 10 step 2) {
//equivalent to i=i+2 in for-i loop
println(i)
}
}
downTo
for counting down tofun main() {
for (i in 10 downTo 1) {
println(i)
}
}
class Person {
var name = ""
var age = 0
fun eat() {
println("" + age + " years old " + name + " is eating")
}
}
fun main() {
val p = Person()
p.age = 2222
p.name = "suraj"
p.eat()
}
class Student {
var sno = ""
var grade = 0
}
open
keyword before the Person class to make it inheritableopen class Person {
var name = ""
var age = 0
fun eat() {
println("" + age + " years old " + name + " is eating")
}
}
class Student: Person() {
var sno = ""
var grade = 0
}
class Student(val sno: String, val grade: Int) Person() {
}
//then call primary constructor like so:
val student = Student("a123", 5)
class Student(val sno: String, val grade: Int) : Person() {
init {
println("sno is " + sno)
println("grade is " + grade)
}
}
2.5.3 Interface
2.5.4 Data Class and Singleton
2.6 Lambda Expression
2.6.1 Creation and Iteration of Collection
2.6.2 Functional APIs of Collections
2.6.3 Java Functional API
2.7 Null Safety
2.7.1 Nullable Type
2.7.2 Nullability Check Tools
2.8 Kotlin Tricks
2.8.1 String Interpolation
2.8.2 Function Default Arguments
2.9 Summary