title: How to print an object's memory address?
body: <p>I saw a question on a java programming test...</p>
  
  <p>If str1 and str2 are both Strings,
  which of the following expressions will correctly determine
  whether they are equal?</p>
  
  <p>(1) (str1 == str2)</p>
  
  <p>(2)  str1.equals(str2)</p>
  
  <p>(3) (str1.compareTo(str2) == 0) </p>
  
  <p>A)1, 2, and 3 will all work</p>
  
  <p>B)2 and 3</p>
  
  <p>C)1 and 2</p>
  
  <p>D)1 and 3</p>
  
  <p>I thought the answer was B. The textbook confirmed this. The teacher's answer key also confirmed this.</p>
  
  <p>I tried testing all of the options. All three worked. What I think the question meant was "If str1 and str2 are both String objects". When I created string objects, it confirmed that B is correct. Below is the code...</p>
  
  <p>What I would like to do is print the memory addresses of the strings to prove in black and white that it is comparing the memory addresses and not the strings. I saw someone do this once by accident and we realized what happened. It was done with very simple code, but we can't remember the accidental syntax.
  I know the address should contain an @ character. I have researched this for a couple hours and I don't think the hashcodes are the addresses because it is the same for both strings and it should be different. Can someone add to my code what is needed to print both addresses?</p>
  
  <pre><code>public class Q1
  {
  public static void main(String[] args)
  {
      String str1 = new String("dog");
      String str2 = new String("dog");
  
      System.out.println(str1 + " &amp; " + str2);
      System.out.println("Hash Codes");
      System.out.println(str1 + Integer.toHexString(str1.hashCode()));
      System.out.println(str2 + Integer.toHexString(str2.hashCode()));
      System.out.println();
  
      if (str1 == str2)
      {
          System.out.println(str1 == str2);
          System.out.println("1 Equal");
          System.out.println();
      }
      else
      {
          System.out.println(str1 == str2);
          System.out.println("1 Not Equal");
          System.out.println();
      }
  
      if (str1.equals(str2))
      {
          System.out.println(str1.equals(str2));
          System.out.println("2 Equal");
          System.out.println();
      }
      else
      {
          System.out.println(str1.equals(str2));
          System.out.println("2 Not Equal");
          System.out.println();
      }
  
      if (str1.compareTo(str2) == 0)
      {
          System.out.println(str1.compareTo(str2) == 0);
          System.out.println("3 Equal");
          System.out.println();
      }
      else
      {
          System.out.println(str1.compareTo(str2) == 0);
          System.out.println("3 Not Equal");
          System.out.println();
      }
  }
  }
  </code></pre>