1.基本类型
不同的类型有不同的功能和属性。Kotlin有推断类型的能力。比如,当你给customers赋值一个Int值时,Kotlin推断customers是一个int类型变量。
fun main(){ var customers = 10 customers = 8 customers = customers + 3 customers += 7 customers -= 3 customers *= 2 customers /= 3 println(customers) }
2. Kotlin共有如下几种基本类型:
Category | Basic types |
Integers | Byte, Short, Int, Long |
Unsigned integers | UByte, UShort, UInt, ULong |
Floating-point numbers | Float, Double |
Booleans | Boolean |
Characters | Char |
Strings | String |
3. 在赋值之前可以使用:指定类型
val d: Int d = 3 val e: String = "hello" println(d) //3 println(e) //hello }