1

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
    var name3 = "123"
    println(name1 === name3) //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.

HARSHIT BAJPAI
  • 361
  • 2
  • 17
  • 1
    Does this answer your question? https://stackoverflow.com/questions/9698260/what-makes-reference-comparison-work-for-some-strings-in-java – gpunto Oct 17 '21 at 15:39
  • @gpunto thanks. It does answer the first part of my question. I am going to add this in the answers. – HARSHIT BAJPAI Oct 17 '21 at 15:43
  • 1
    I don't really understand why `val` and `var` should be considered different. This is like asking why `this.myProperty == localVariable` is allowed if properties/fields and local variables are essentially different things. When checking equality it doesn't matter **where** the data is stored or **how** it is stored. We are checking **what** is stored, we look for the contents. – broot Oct 17 '21 at 20:24

2 Answers2

2

So as far as string referential equality check 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.

LarsH
  • 27,481
  • 8
  • 94
  • 152
HARSHIT BAJPAI
  • 361
  • 2
  • 17
1

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.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • Well, `val` and `var` aren't references either. They're keywords that designate variables (or properties) as being read-only or reassignable. The variables could hold references to objects, or they could be of primitive types. Maybe that's what you meant. – LarsH Jan 05 '23 at 17:13