danger mouse
danger mouse

Reputation: 1497

Java String references

When I run the following code

public class Test {
    public static void main(String[] args) {
        System.out.println(args[0]);
        System.out.println("testing");
        System.out.println(args[0] == "testing");
    }
}

using

java Test testing

at the command line, it prints the following:

testing
testing
false

My understanding is that, when comparing Strings, == compares the references of the Strings, not their values. For the two Strings in the following code, how can I find out what their references are?

System.out.println(args[0] == "testing");

Upvotes: 0

Views: 125

Answers (2)

fge
fge

Reputation: 121720

My understanding is that, when comparing Strings, == compares the references of the Strings, not their values

Indeed; but then replace String with Object in the sentence above, or indeed any class implementing Object (which means, in essence, each and every Java class) and the above sentence will hold...

Except that "values" is too vague.

There is a fundamental contract in Java, and that is Object's .equals() and .hashCode(). They are intrinsically linked to one another. What they essentially define is an equivalence contract.

The base equivalence contract for Object is simple, really; given two Objects o1 and o2, it is true that:

o1.equals(o2)

is strictly equivalent to:

o1 == o2

But it is only because for Object, .equals() is defined as such:

// pseudo code...
public boolean equals(final Object o)
{
    returns this == o;
}

Now, of course, this won't stand true for String. A String has state (its characters), an Object doesn't. Therefore String "refines" .equals(). Which means that even if you have:

String s1 = "hello world";
String s2 = "hello world";

it is not true that:

s1 == s2

(save for particular, irrelevant scenarios), but it is true that:

s1.equals(s2)

As a "side effect", this also means that String also has to "refine" .hashCode() as well, since the contract explicitly stipulates that two objects which are equals() must have the same hashCode().

Upvotes: 0

Code Whisperer
Code Whisperer

Reputation: 1041

You can use equals(), compareTo(), equalsIgnoreCase() when comparing Strings.

You only need references if you want to do something with memory locations which I am not sure why you would want to do.

That defeats the purpose of Java and you should switch to C if that is what you are looking for or if you want to perform actions on memory addresses.

Upvotes: 2

Related Questions