Home > OS >  Referential vs Structural equality in Kotlin in case of strings
Referential vs Structural equality in Kotlin in case of strings

Time:10-18

My current understanding of structural equality is that it compares the type and the content.

For Referential equality, it compares the address of the two objects. Also, in the case of a primitive type of var, it will print true if the content is the same.

According to the doc here --> numbers, characters and booleans can be represented as primitive values at runtime - but to the user, they look like ordinary classes.

So String should be treated as objects at runtime.

But I get true when comparing referentially two strings that have the same content.

fun main(){
    val name1 = "123"
    val name2 = "123"

    println(name1 == name2) //true, structural equality which is same type and same content (same as equals)

    // checks if left and right are same object --> referential equality
    println(name1 === name2) //true ????????
    // should print false. String is basic type and not primitve
    
    var Arr: IntArray = intArrayOf(1,2,3)
    var Arr2: IntArray = intArrayOf(1,2,3)
    println(Arr === Arr2) // prints false, arrays are basic type
}

Also, why doesn't equality in kotlin of both types differentiates between val and var? They are two different types of objects at their core.

Can someone point out where am I going wrong with this? I might be missing something pretty obvious here.

CodePudding user response:

val and var are not different types of objects. They are references, not objects at all. They have nothing to do with the behavior of the objects they are referring to. When you compare two objects, the types of variables and properties that are referencing them is not a factor whatsoever. When you call equals on one object, the runtime only uses the reference to find the object in memory. Only then is its function called.

CodePudding user response:

So as far as string comparison goes, I think it does the same thing as java in that it adds everything to the string constant pool.

Here is the thread for referential string comparison in java (done by ==) - What makes reference comparison (==) work for some strings in Java?

For Val vs Var the answer by @Tenfour04 below explains the logic.

  • Related