-1

When I try to print custom object in scala it gives its memory address

object Test; println(Test)<br/>
Output : Test$@250f9a46

when It comes to list it prints string as toString been overridden

println(List(1,2,3))<br/>
output : List(1, 2, 3)

Is there a way to get the address?

Dinidu Hewage
  • 2,169
  • 6
  • 40
  • 51
  • 1
    This is more JVM question than just Scala one. This question shows how to achieve that: http://stackoverflow.com/questions/8820164/is-there-a-way-to-get-a-reference-address – Mateusz Kubuszok Mar 12 '17 at 14:11
  • Possible duplicate of [Is there a way to get a reference address?](http://stackoverflow.com/questions/8820164/is-there-a-way-to-get-a-reference-address) – matfax Mar 12 '17 at 14:27

1 Answers1

4

It's actually not the memory address you're getting by the default toString method but the hashcode of the Object. (The memory address might also change during runtime because the garbage collector moves the object between different generation space.)

The toString implementation in java.lang.Object is

return getClass().getName() + "@" + Integer.toHexString(hashCode());

So you could use this code to get a similar result.

Harald Gliebe
  • 7,236
  • 3
  • 33
  • 38