1. kotlin 数值型
fun main() {
val a: Byte = 1
val b: Short = 2
val c: Int = 3
val c2 = 3
val d: Long = 4
val d2 = 4L
val e: Float = 3.14f
val e2 = 3.14f
val f: Double = 3.14
}
2. kotlin 布尔型
fun main() {
val state: Boolean = true
val state2 = false
}
3. kotlin 字符型
fun main() {
val aChar: Char = 'a'
val covChar: Char = '\r'
val ch: Char = '\u5475'
println(ch)
}
4. kotlin 字符串
fun main() {
val str1: String = "str1"
val str2 = "str2"
val str3 = "$str1 length: ${str1.length}"
println(str3)
val str4 = """
I am a programmer,
175cm
""".trimMargin()
println(str4)
val str5 = """
|I am a programmer,
|175cm
""".trimMargin()
println(str5)
val str6 = """
&I am a programmer,
&175cm
""".trimMargin("&")
println(str6)
}
5. kotlin 可空类型
fun main() {
var aa: Byte? = null
aa = 1
var bb: Short? = null
bb = 2
var cc: Int? = null
cc = 3
var dd: Long? = null
dd = 4
var ee: Float? = null
ee = 5f
var ff: Double? = null
ff = 6.0
var gg: Boolean? = null
gg = false
}
6. kotlin 类型相互转换
fun main() {
var aa: Byte? = null
aa.toInt()
aa.toShort()
aa.toLong()
aa.toFloat()
aa.toDouble()
cc = null
cc?.toLong()
cc?.toString()
}
7. kotlin 二进制、十六进制
fun main() {
val bval1 = 0b101010
val bval2 = 0B1010110
var x = -0b11111111
println(x)
val tenVal1 = 0x132
val tenVal2 = 0X1d
}
附 Github 源码:
TestBasicType.kt
|