0

described above. for example.

String s = "abc";
System.out.println(s); // this method will output the string, not the address

so how to view the address, thanks in advance.

  • 5
    Does this answer your question? [How can I get the memory location of a object in java?](https://stackoverflow.com/questions/7060215/how-can-i-get-the-memory-location-of-a-object-in-java) – Rayan Ral Jul 07 '20 at 04:12

2 Answers2

0

Java has no concept of "address", so it is impossible to get the address of any object, including strings.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • thanks. if execute `System.out.println(new int[] {1, 2, 3});`, it outputs `[I@621be5d1`. can the String class output it like that format? – obitoquilt Jul 07 '20 at 04:59
0

if u want an answer as u mentioned in the comment, Actually there is a way to do it that way.

String s = new String("abc");
System.out.println(Integer.toHexString(s.hashCode()));

this will return

1ae66

This answer was historically accepted as correct and will only work for classes that didn't override the hashCode() method

egemenakturk
  • 365
  • 1
  • 17
  • 1
    System.identityHashCode() gives you always the original implementation :) https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#identityHashCode-java.lang.Object- – Boris Brodski Jul 07 '20 at 08:57