Basic Syntax

Basic Syntax

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.

See Packages.

Defining functions

Function having two Int parameters with Int return type:

//sampleStart
fun sum(a: Int, b: Int): Int {
    return a + b
}
//sampleEnd

fun main(args: Array<String>) {
    print("sum of 3 and 5 is ")
    println(sum(3, 5))
}

Function with an expression body and inferred return type:

//sampleStart
fun sum(a: Int, b: Int) = a + b
//sampleEnd

fun main(args: Array<String>) {
    println("sum of 19 and 23 is ${sum(19, 23)}")
}
登录查看完整内容