0

I saw a question on a java programming test...

If str1 and str2 are both Strings, which of the following expressions will correctly determine whether they are equal?

(1) (str1 == str2)

(2) str1.equals(str2)

(3) (str1.compareTo(str2) == 0)

A)1, 2, and 3 will all work

B)2 and 3

C)1 and 2

D)1 and 3

I thought the answer was B. The textbook confirmed this. The teacher's answer key also confirmed this.

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...

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?

public class Q1
{
public static void main(String[] args)
{
    String str1 = new String("dog");
    String str2 = new String("dog");

    System.out.println(str1 + " & " + 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();
    }
}
}
user2379362
  • 7
  • 1
  • 2
  • 1
    The answer is **1** for *memory address*. Only `==` tests reference equality. And **2** is a valid test for *value* equality. – Elliott Frisch Mar 16 '16 at 01:06
  • 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) – Erwin Bolwidt Mar 16 '16 at 01:33
  • Or if you want a less `Unsafe` answer: http://stackoverflow.com/questions/1961146/memory-address-of-variables-in-java – Erwin Bolwidt Mar 16 '16 at 01:34

1 Answers1

0

Simple answer: No, you can't.

Object.hashCode() may return internal address of an object, but it's implementation-dependent. You can check it at Javadocs.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

For example, by default HotSpot returns random number from Object.hashCode(), see the Source code.

Nier
  • 638
  • 7
  • 15
  • The questions wasn't whether `Object.hashCode()` returns the memory address, but whether there is any method to get the memory address. And there are ways, see the duplicate that I suggested for this question (in the comments below it) – Erwin Bolwidt Mar 16 '16 at 01:48
  • I just saw that link, didn't know there's a Unsafe API and that's better answer to this question. I mentioned `hashCode` because it says it may return internal address in javadocs, however it's not reliable. – Nier Mar 16 '16 at 02:08