Summarize of Kotlin basic grammar.

2.1 Basic Example

1. Unit

The Unit return type declaration is optional for functions. The following code are equivalent.

1
2
3
4
5
fun printHello(name : String?) : Unit{
if(name != null){
println("Hello $name")
}
}
1
2
3
4
5
fun printHello(name : String?){
if(name != null){
println("Hello $name")
}
}

2. Single-Expression function

When a function returns a single expression, curly braces can be omitted and the body is specified after = symbol.

1
fun double (x : Int) : Int = x *2

And declaring the return type is optional when this can be inferred by compiler.

1
fun double (x : Int) = x * 2

3. NULL hold

In Kotlin, the type system distinguishes between references that can hold null and those that can not.
If use nullable value, should use ?.

1
2
var a : String = "123"
a = null // compile error
1
2
var a : String? = "123"
a = null // working

4. Comparison

In Kotlin, Actually check for equality of values. By convention, an expression like a == b is translated to

1
a?.equals(b) ?: (b === null)