kotlin中!!.和?.的区别

在Kotlin中!!跟?都是用于判断空参数异常的

?.意思是这个参数可以为空,并且程序继续运行下去

!!.的意思是这个参数如果为空,就抛出异常

Kotlin 中的数字没有隐式拓宽转换

Double 参数的函数只能对 Double 值调用,而不能对 FloatInt 或者其他数字值调用

  1. fun main() {
  2. fun printDouble(d: Double) { print(d) }
  3. val i = 1
  4. val d = 1.1
  5. val f = 1.1f
  6. printDouble(d)
  7. // printDouble(i) // 错误:类型不匹配
  8. // printDouble(f) // 错误:类型不匹配
  9. }