相等判断


结构相等

三种判断方式

    var str = "H"
    var str2: String = "H"
    println("str is equal str2? ${str.equals(str2)}")
    println("str == str2? ${str == str2}")
    println("str === str2? ${str === str2}")

    var str3: String? = "H"
    println("Compare str3--------------------------------------")
    println("str is equal str3? ${str.equals(str3)}")
    println("str == str3? ${str == str3}")
    println("str === str3? ${str === str3}")

    var t1 = Table("H")
    var t2 = Table("H")
    var t3: Table? = Table("H")
    println("------t1 vs t2---------")
    println("t1 is equal t2? ${t1.equals(t2)}")
    println("t1 == t2? ${t1 == t2}")
    println("t1 === t2? ${t1 === t2}")

    println("------t1 vs t3---------")
    println("t1 is equal t3? ${t1.equals(t3)}")
    println("t1 == t3? ${t1 == t3}")
    println("t1 === t3? ${t1 === t3}")

    /**
str is equal str2? true
str == str2? true
str === str2? true
Compare str3--------------------------------------
str is equal str3? true
str == str3? true
str === str3? true
------t1 vs t2---------
t1 is equal t2? false
t1 == t2? false
t1 === t2? false
------t1 vs t3---------
t1 is equal t3? false
t1 == t3? false
t1 === t3? false
    */

可以看出,当是string时,只要值一样,就一定相等,但是对于自己定义的class ,都是不相等的

  • 浮点数的比较

当然这是错误的,本来是不相等的,但是打印出来是相等的 奇怪了吧,这是由于double小数点后面有浮动

    var a: Double = 1.100000000000000000000000000000000001
    var b: Double = 1.1
    println("a==b? ${a == b}")
    println("a==b? ${a === b}")
 /**
 打印
a==b? true
a==b? true
 */

results matching ""

    No results matching ""