Kotlin学习笔记——第1天
1. 方法
fun main(){ println("Hello, World!") }
- fun 方法声明关键字
- main() 程序入口
- 方法体用{}包含
- println() 、print() 标准输出方法
2. 变量
任何程序都需要存储数据,变量可以帮助我们做到这一点。在Kotlin中,有两种变量声明方法:
- val 只读变量
- var 可变变量
val popcorn = 5 val hotdog = 7 var customers = 10 customers = 8 println(customers)
如非必需,变量尽量声明为val。
3. 字符串格式化
val customers = 10 println("There are $customers customers") println("There are ${customers + 1} customers")
- 格式化中使用$引用变量
- 如果格式化表达式中需要插入代码,在$后使用{}包含