0

I have a List(fruits) that I want to find and print the reference(memory address) of it, How can I?

val fruits:List[String]=List("apple","orange","pear")

1 Answers1

0

It is not really clear what you mean by references. A String is already A reference. If you want to unrefine it ("forget" that the underlying structure is a String) and just have a pure reference you can rely on the fact that the type parameter of a list if covariant and List[String] is a subtype of List[AnyRef].

val refs: List[AnyRef] = fruits

Not sure why this would be useful tho. Not much you can do with references.

  • I mean the memory address of fruit. Can I find it? – sahar karimzadeh Nov 26 '21 at 16:44
  • It is impossible to get an address of any primitive variable or object on the JVM. The closes thing is the object hashcode with System.identityHashCode(a). which is related to its address, but there are no guarantees here. – Navid Jalali Dec 05 '21 at 02:28
  • Ok, I mean Something like this case in Java, That it is the memory reference in a specific time, Can I have it? Is System.identityHashCode(a) something like that? – sahar karimzadeh Dec 09 '21 at 06:02