= 1 // A boxed Int (java.lang.Integer) val b: Long? = a // implicit conversion yields a boxed Long (java.lang.Long) print(a == b) // Surprise! This prints "false" as Long's equals() check for other part to be L
Kotlin的基本数据类型包括Byte、Short、Int、Long、Float、Double等,具有明确的位宽和取值范围。Kotlin中字符用Char类型,布尔用Boolean,字符串用String且不可变,支持模板表达式和多种操作。
val a: Int = 10000 print(a == a) // 输出“true” val boxedA: Int? = a val anotherBox...
但是不包括基本类型:byte int long等,基本类型对应的包装类是引用类型,其父类是Object。而在Kotlin中,直接统一——所有类型都是引用类型,统一继承父类Any。Any是Java的等价Object类。但是跟Java不同的是,Kotlin中语言内部的类型和用户定义类型之间,并没有像Java那样划清界限。它们是同一类型层次结构的一部分。
val a: Int? = 1 // A boxed Int (java.lang.Integer) val b: Long? = a // implicit conversion yields a boxed Long (java.lang.Long) print(a == b) // Surprise! This prints "false" as Long's equals() check for other part to be Long as well ...
res15: String = Hello,World1 scala> 1+"Hello,World" res16: String = 1Hello,World 1. 2. 3. 4. 5. 但是在Kotlin中, 由于Int类型没有对+实现重载,所以情况是这样 >>> "Hello,World"+1 Hello,World1 ...
funmain(args:Array<String>){ println("Hello, World!") } main()函数是程序的入口点。 使用fun关键字声明函数。 在 Kotlin 中,我们不必将函数放入类中。println()函数将消息打印到控制台。main()函数将字符串数组作为参数。 请注意,在 Kotlin 中,类型后跟在冒号后面的变量名。
package com.mikyou.kotlin.lambda.simple typealias Sum = (Int, Int, Int) -> Int fun main(args: Array<String>) { val sum: Sum = { a, b, c ->//定义一个很简单的三个数求和的lambda表达式 a + b + c } println(sum.invoke(1, 2, 3)) } lambda调用处反编译后的代码 package com....
printDouble(i)// 错误:类型不匹配 printDouble(f)// 错误:类型不匹配 4.kotlin数字定义不支持八进制 1 2 3 4 十进制:123 -- Long类型 123L 十六进制:0x0f 二进制: 0b001 5.当采用可空的引用(Int?)或泛型,后者情况会把数字装箱,装箱的数字保留相等性,但不一定保留同一性 ...
1 // 假设代码,没有实际编译: 2 val a: Int? = 1 // 装箱为Int (java.lang.Integer) 3 val b: Long? = a // 隐含转换装箱为Long (java.lang.Long) 4 print(a == b) // 令人惊讶! 当equals()检查其它部分不是Long,就打印"false" ...