Kotlin数据类型(二 )

Kotlin数据类型(二 )_第1张图片
思维导图

微语:有些事情,你想忘记的就会忘记,如果忘记不了,那就不要忘记,因为忘记是不需要努力的。 ---《春娇与志明》

一、可空型

Kotlin与Java最大的一个区别就是Kotlin为了尽量避免空引用所带来的危险,如NullPointerException(NPE)的出现。在Kotlin中,类型系统将可空类型和不可空类型进行了区分,例如,String为不可空类型,String?为可空类型,如果将不可空类型赋值为null将会编译不通过。

  • 问题:求str的长度?此时如果str = null,程序运行的时候就会抛出空指针错误,为了有效避免空指针的出现,在Kotlin中这种写法在编译的时候就会报错。那么怎样允许str为空呢?
fun main(args: Array) {
    val str = getString()
    println(str.length)
}
fun getString():String{
    return null  //此时编译器报错
}
  • 解决方法一:在String后面加?,表示这个函数的返回值可以为空,但是Kotlin是聪明的,当str可以为空的时候str.length就可能出现空指针的错误,所以编译器在str.length这里还是会报错,此时在str后面加上两个感叹号!!就可以了,表示我们知道str可以为空,并且信任这样的做法。(此时运行你的程序就准备你的程序Crash吧,哈哈)
fun main(args: Array) {
    val str = getString()
    println(str!!.length)
}
fun getString():String?{
    return null  
}
  • 解决方法二:添加if判断,因为Kotlin是智能的所以能识别出这种判断,这样我们的程序就可以正常编译了,但是这种做法是毫无意义的,因为我们完全可以用Kotlin中的空类型。
fun main(args: Array) {
  val str = getString()
    if (str == null){
        println("str的值为空,出现异常")
    }else{
        println(str.length)
    }
}
fun getString():String?{
    return null 
}

二、两个数比较

  • == 比较的是内容,相当于Java中的equals()方法
  • === 比较的是对象是否相同
  val a:Int = 1000
  val aBox:Int = a
  val anthorBox:Int = a
  println(aBox == anthorBox )
 println(aBox === anthorBox )
  val bBox:Int? = a
  val anthorbBox:Int? = a
  println(bBox === anthorbBox)
运行结果:
true
true
false

注意:之所以第三个结果为false是因为Int?进行了装箱操作

  • 在Kotlin中,存在数字的装箱,但是不存在拆箱。不存在和Java中的类似int是数据类型,Integer是整型的引用类型。

  • 在Kotlin中要实现装箱操作。首先要了解空类型。即类似Int?(只限数值类型)这样的。

三、类型转换(显示转换)

  • 由于不同的表示方式,较小类型并不是较大类型的子类型,较小的类型不能隐式转换为较大的类型。 这意味着在不进行显式转换的情况下我们不能把 Byte 型值赋给一个 Int 变量。
val b: Byte = 1 // OK, 字面值是静态检测的
val i: Int = b // 错误
我们可以代用其toInt()方法。
val b: Byte = 1 
val i: Int = b.toInt() // OK
  • 每种数据类型都有下面的这些方法,可以转化为其它的类型:
toByte(): Byte
toShort(): Short
toInt(): Int
toLong(): Long
toFloat(): Float
toDouble(): Double
toChar(): Char
  • 有些情况下也是可以使用自动类型转化的(聪明的Kotlin),前提是可以根据上下文环境推断出正确的数据类型而且数学操作符会做相应的重载。例如下面:
val l:Long = 1L + 3 // Long + Int => Long
运行结果:4

四 、位操作符

  • Kotlin中对于按位操作,和Java是有很大的差别的。Kotlin中没有特殊的字符,但是只能命名为可以以中缀形式调用的函数,下列是按位操作的完整列表(仅适用于整形(Int)和长整形(Long)):

    • shl(bits) => 有符号向左移 (类似Java的<<)
    • shr(bits) => 有符号向左移 (类似Java的>>)
    • ushr(bits) => 有符号向左移 (类似Java的>>>)
    • and(bits) => 位运算符 and (同Java中的按位与)
    • or(bits) => 位运算符 or (同Java中的按位或)
    • xor(bits) => 位运算符 xor (同Java中的按位异或)
    • inv() => 位运算符 按位取反 (同Java中的按位取反)
  • 下面附上Kotlin中关于位操作符的源码:

/** Shifts this value left by [bits]. */
public infix fun shl(bitCount: Int): Int
/** Shifts this value right by [bits], filling the leftmost bits with copies of the sign bit. */
public infix fun shr(bitCount: Int): Int
/** Shifts this value right by [bits], filling the leftmost bits with zeros. */
public infix fun ushr(bitCount: Int): Int
/** Performs a bitwise AND operation between the two values. */
public infix fun and(other: Int): Int
/** Performs a bitwise OR operation between the two values. */
public infix fun or(other: Int): Int
/** Performs a bitwise XOR operation between the two values. */
public infix fun xor(other: Int): Int
/** Inverts the bits in this value. */
public fun inv(): Int
  • demo:
var operaNum: Int = 4
var shlOperaNum = operaNum shl(2)
var shrOperaNum = operaNum shr(2)
var ushrOperaNum = operaNum ushr(2)
var andOperaNum = operaNum and(2)
var orOperaNum = operaNum or(2)
var xorOperaNum = operaNum xor(2)

println("shlOperaNum => $shlOperaNum \n " +
        "shrOperaNum => $shrOperaNum \n " +
        "ushrOperaNum => $ushrOperaNum \n " +
        "andOperaNum => $andOperaNum \n " +
        "orOperaNum => $orOperaNum \n " +
        "xorOperaNum => $xorOperaNum \n ")

运行结果:
shlOperaNum => 16 
shrOperaNum => 1 
ushrOperaNum => 1 
andOperaNum => 0 
orOperaNum => 6 
xorOperaNum => 6 

你可能感兴趣的:(Kotlin数据类型(二 ))