Kotlin 简明教程

Kotlin - Basic Syntax

Kotlin Program Entry Point

Kotlin 应用程序的入口点是 main() 函数。函数可以定义为一个旨在执行特定任务的代码块。

An entry point of a Kotlin application is the main() function. A function can be defined as a block of code designed to perform a particular task.

让我们从一个基本的 Kotlin 程序开始,在标准输出上打印“Hello,World!”:

Let’s start with a basic Kotlin program to print "Hello, World!" on the standard output:

fun main() {
   var string: String  = "Hello, World!"
   println("$string")
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

Hello, World!

Entry Point with Parameters

还有另一种形式的 main() 函数接受不同的字符串参数,如下所示:

Another form of main() function accepts a variable number of String arguments as follows:

fun main(args: Array<String>){
    println("Hello, world!")
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

Hello, World!

如果你已观察,很显然两个程序生成了相同输出,因此在 main() 函数从 Kotlin 版本 1.3 开始传递参数是可选的。

If you have observed, its clear that both the programs generate same output, so it is very much optional to pass a parameter in main() function starting from Kotlin version 1.3.

print() vs println()

print() 是 Kotlin 中的函数,用于将它的参数打印至标准输出,以类似方式 println() 是另一个将它的参数打印至标准输出的函数,但它也在输出中添加换行符。

The print() is a function in Kotlin which prints its argument to the standard output, similar way the println() is another function which prints its argument on the standard output but it also adds a line break in the output.

我们尝试以下程序以理解这两个重要函数之间的差异:

Let’s try the following program to understand the difference between these two important functions:

fun main(args: Array<String>){
    println("Hello,")
    println(" world!")

    print("Hello,")
    print(" world!")
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

Hello,
 world!
Hello, world!

这两个函数(print() 和 println())均可用于打印数字以及字符串,同时执行任何数学计算,如下所示:

Both the functions (print() and println()) can be used to print numbers as well as strings and at the same time to perform any mathematical calculations as below:

fun main(args: Array<String>){
    println( 200 )
    println( "200" )
    println( 2 + 2 )

    print(4*3)
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

200
200
4
12

Semicolon (;) in Kotlin

Kotlin 代码语句不需要分号 (;) 来结束语句,而诸如 Java、C++、C# 等许多其他编程语言需要分号。

Kotlin code statements do not require a semicolon (;) to end the statement like many other programming languages, such as Java, C++, C#, etc. do need it.

虽然你可以通过带或不带分号编译并运行 Kotlin 程序,如下所示:

Though you can compile and run a Kotlin program with and without semicolon successfully as follows:

fun main() {
    println("I'm without semi-colon")

    println("I'm with semi-colon");
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

I'm without semi-colon
I'm with semi-colon

因此作为良好的编程实践,不建议在 Kotlin 语句的末尾添加分号。

So as a good programming practice, it is not recommended to add a semicolon in the end of a Kotlin statement.

Packages in Kotlin

Kotlin 代码通常定义在包中,虽然包规范是可选的。如果你不在源文件中指定包,它的内容会转到默认包。

Kotlin code is usually defined in packages though package specification is optional. If you don’t specify a package in a source file, its content goes to the default package.

如果我们在 Kotlin 程序中指定包,则它在文件顶部指定,如下所示:

If we specify a package in Kotlin program then it is specified at the top of the file as follows:

package org.tutorialspoint.com

fun main() {
    println("Hello, World!")
}

当你运行上述 Kotlin 程序时,它将生成以下输出:

When you run the above Kotlin program, it will generate the following output:

Hello, World!