'基础数据类型'
This commit is contained in:
parent
4a966e2462
commit
ff5d7716dc
Binary file not shown.
Binary file not shown.
BIN
out/production/kotlin-study-demo/basic/VariableKt.class
Normal file
BIN
out/production/kotlin-study-demo/basic/VariableKt.class
Normal file
Binary file not shown.
66
src/basic/variable.kt
Normal file
66
src/basic/variable.kt
Normal file
@ -0,0 +1,66 @@
|
||||
package basic
|
||||
|
||||
import sun.rmi.runtime.Log
|
||||
|
||||
/**
|
||||
* 变量基础定义
|
||||
* 基本数据类型 Int Long Float Double String
|
||||
* 关键字 var 可变变量
|
||||
* 关键字 val 不可变变量
|
||||
* 关键字 变量: 数据类型 = 默认值
|
||||
*/
|
||||
|
||||
fun main() {
|
||||
|
||||
/**
|
||||
* 可变变量
|
||||
*/
|
||||
|
||||
// 定义变量 声明变量类型 并初始化
|
||||
var a: Int = 1
|
||||
a += 6
|
||||
println(a)
|
||||
|
||||
// 不声明类型 自动推断
|
||||
var b = 2L
|
||||
println(b)
|
||||
|
||||
// 未初始化变量值 必须声明变量类型
|
||||
var c: Int
|
||||
c = 3
|
||||
println(c)
|
||||
|
||||
// 打印拼接
|
||||
println("$a + $b + $c")
|
||||
|
||||
/**
|
||||
* 不可变变量(常量)
|
||||
*/
|
||||
|
||||
// 声明 初始化
|
||||
val d: Int = 123
|
||||
// 以下写法或报错 不可变变量不能二次赋值
|
||||
//d += 1
|
||||
println(d)
|
||||
// 双精度整型
|
||||
val i: Long = 6L
|
||||
println(i)
|
||||
|
||||
// 自动推断类型
|
||||
val e = "rainerosion"
|
||||
println(e)
|
||||
|
||||
// 字符串
|
||||
val f: String
|
||||
|
||||
// 单精度浮点数
|
||||
val g: Float = 3.1f
|
||||
|
||||
// 双精度 浮点数
|
||||
val h: Double = 4.0
|
||||
println(h)
|
||||
|
||||
// 不可变字符初始化
|
||||
f = g.toString()
|
||||
print(f)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user