Please check Kotlin Basic Syntax for further information.
Package specification should be at the top of the source file. It is not required to match directories and packages: source files can be placed arbitrarily in the file system.
package my.demo
import kotlin.text.*
// ...
An entry point of a Kotlin application is the main
function.
fun main() {
println("Hello world!")
}
print
prints its argument to the standard output.
print("Hello")
println
prints its arguments and adds a line break, so that the next thing you print appears on the next line.
println("Hello world!")
println(42)
A function with two Int
parameters and Int
return type.
fun sum(a: Int, b: Int): Int {
return a + b
}
A function body can be an expression. Its return type is inferred.
fun sum(a: Int, b: Int) = a + b
Read-only local variables are defined using the keyword val
. They can be assigned a value only once.
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
Variables that can be reassigned use the var
keyword.
var x = 5 // `Int` type is inferred
x += 1
You can declare variables at the top level.
val PI = 3.14
var x = 0
fun incrementX() {
x += 1
}
To define a class, use the class
keyword.
class Shape
Properties of a class can be listed in its declaration or body.
class Rectangle(var height: Double, var length: Double) {
var perimeter = (height + length) * 2
}
The default constructor with parameters listed in the class declaration is available automatically.
val rectangle = Rectangle(5.0, 2.0)
println("The perimeter is ${rectangle.perimeter}")
Inheritance between classes is declared by a colon (:
). Classes are final by default; to make a class inheritable, mark it as open
.
open class Shape
class Rectangle(var height: Double, var length: Double): Shape() {
var perimeter = (height + length) * 2
}
Just like most modern languages, Kotlin supports single-line (or end-of-line) and multi-line (block) comments.
// This is an end-of-line comment
/* This is a block comment
on multiple lines. */
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"
fun maxOf(a: Int, b: Int): Int {
if (a > b) {
return a
} else {
return b
}
}
val items = listOf("apple", "banana", "kiwifruit")
for (item in items) {
println(item)
}
//or
val items = listOf("apple", "banana", "kiwifruit")
for (index in items.indices) {
println("item at $index is ${items[index]}")
}
Check if a number is within a range using in
operator.
val x = 10
val y = 9
if (x in 1..y+1) {
println("fits in range")
}
A reference must be explicitly marked as nullable when null
value is possible. Nullable type names have ?
at the end.
Return null if str does not hold an integer:
fun parseInt(str: String): Int? {
// ...
}
The is
operator checks if an expression is an instance of a type. If an immutable local variable or property is checked for a specific type, there's no need to cast it explicitly:
fun getStringLength(obj: Any): Int? {
if (obj is String) {
// `obj` is automatically cast to `String` in this branch
return obj.length
}
// `obj` is still of type `Any` outside of the type-checked branch
return null
}
Check out the following websites for further information: