freeCodeCamp/guide/chinese/kotlin/hello-world/index.md

1.9 KiB
Raw Blame History

title localeTitle
Hello World in Kotlin Kotlin的Hello World

Hello World程序是一个非常简单的程序它输出字符串“Hello World”。它通常用于显示编程语言的基本语法。

在本教程中我们将分析用Kotlin编写的Hello World程序的语法。

如果您还没有安装Kotlin请查看本教程https//guide.freecodecamp.org/kotlin

你好世界计划

// This is a simple Hello World program written in Kotlin 
 
 fun main(args : Array<String>) { 
    println("Hello, World!") 
 } 

正如您所料当您运行此程序时输出应为“HelloWorld”。

句法

注释

// This is a simple Hello World program written in Kotlin 

注释是由开发人员编写的文本,其目的是使代码更容易被其他开发人员理解。 在Kotlin中注释可以是单行注释使用//)或多行注释(使用/ ** /)。

// Single line comment 
 
 /* This is a 
 Multi-line comment 
 */ 

主功能

fun main(args : Array<String>) {...} 

main函数是一个强制函数它告诉编译器它应该从哪里开始执行我们的代码。它将一个字符串数组作为参数并返回类型Unit它对应于Java等语言中的void类型。 正如我们所看到的,函数是使用关键字fun ,其主体应该用花括号括起来。

没有显式声明的返回类型的函数将返回Unit类型,因此,上面的代码相当于

fun main(args : Array<String>): Unit {...} 

打印声明

println函数将字符串作为参数并将其打印到屏幕上。在这种情况下我们打印字符串“HelloWorld”。请注意字符串文字使用双引号"String"

如果您想了解更多关于Kotlin语法的知识并开始撰写精彩的程序您应该查看非常棒的Kotlin官方文档https//kotlinlang.org/docs/reference/

希望你喜欢这个教程, 干杯