Basic Syntax in Kotlin
Defining packages
Package specification should be at the top of the source file:
package my.demo
import java.util.*
// ...
It is not required to match directories and packages: source files can be placed arbitrarily in the file system.
Defining functions
Function having two Int parameters with Int return type:
fun sum(a: Int, b: Int): Int {
return a + b
}
fun main(args: Array<String>) {
print("sum of 3 and 5 is ")
println(sum(3, 5))
}
Result:sum of 3 and 5 is 8
Function with an expression body and inferred return type:
fun sum(a: Int, b: Int) = a + b
fun main(args: Array<String>) {
println("sum of 19 and 23 is ${sum(19, 23)}"
}
Result:sum of 19 and 23 is 42
Function returning no meaningful value:
fun printSum(a: Int, b: Int): Unit {
println("sum of $a and $b is ${a + b}"
}
fun main(args: Array<String>) {
printSum(-1, 8
Result:sum of -1 and 8 is 7
Unit return type can be omitted:
fun printSum(a: Int, b: Int) {
println("sum of $a and $b is ${a + b}"
}
fun main(args: Array<string>) {
printSum(-1, 8
Result:sum of -1 and 8 is 7
Defining local variables
Assign-once (read-only) local variable:
fun main(args: Array<String>) {
val a: Int = 1 // immediate assignment
val b = 2 // `Int` type is inferred
val c: Int // Type required when no initializer is provided
c = 3 // deferred assignment
println("a = $a, b = $b, c = $c"
Result:a = 1, b = 2, c = 3
Mutable variable:
fun main(args: Array<String>) {
var x = 5 // `Int` type is inferred
x += 1
println("x = $x"
Result:x = 6
Comments:
// This is an end-of-line comment
/* This is a block comment
on multiple lines. */
Unlike Java, block comments in Kotlin can be nested.
Using string templates:
fun main(args: Array<String>) {
var a = 1
//simple name in template:
val s1 ="a is $a"
a = 2
// arbitrary expression in template:
val s2 =${s1.replace("is", "was")}, but now is $a
println(s2)
}
Result:a was 1, but now is 2
Using conditional expressions:
fun maxOf(a: Int, b: Int): Int {
if (a > b) {
return a
} else {
return b
}
}
fun main(args: Array<String>) {
println("max of 0 and 42 is ${maxOf(0, 42)}" )
}
Result:max of 0 and 42 is 42
Using if as an expression:
fun maxOf(a: Int, b: Int) = if (a > b) a else b
fun main(args: Array<string>) {
println( "max of 0 and 42 is ${maxOf(0, 42)}")
}
Result:max of 0 and 42 is 42
Using nullable values and checking for null:
A reference must be explicitly marked as nullable when null value is possible.
Return null if str does not hold an integer:
fun parseInt(str:String Int? {
// ...
}
Use a function returning nullable value:
fun parseInt(str: String): Int? {
return str.toIntOrNull()
}
fun printProduct(arg1: String, arg2: String) {
val x = parseInt(arg1)
val y = parseInt(arg2)
// Using `x * y` yields error because they may hold nulls.
if (x != null && y != null) {
// x and y are automatically cast to non-nullable after null check
println(x * y)
}
else {
println("either '$arg1' or '$arg2' is not a number")
}
}
fun main(args: Array<String>) {
printProduct("6", "7")
printProduct("a", "7")
printProduct("a", "b")
}
Result:
42
either 'a' or '7' is not a number
either 'a' or '7' is not a number
OR
fun parseInt(str: String): Int? {
return str.toIntOrNull()
}
fun printProduct(arg1: String, arg2: String) {
val x = parseInt(arg1)
val y = parseInt(arg2)
//...
if (x == null) {
println("Wrong number format in arg1: '${arg1}'")
return
}
if (y == null) {
println("Wrong number format in arg2: '${arg2}'")
return
}
// x and y are automatically cast to non-nullable after null check
println(x * y)
}
fun main(args: Array<String>) {
printProduct("6", "7")
printProduct("a", "7")
printProduct("99", "b")
}
Result:
42
Wrong number format in arg1: 'a'
Wrong number format in arg2: 'b'
Comments
Post a Comment
Please Comment if Any Query !!