-1

I have the following code:

public class Porow{
    public static void main (String[] args){
        String s1 = "foo";
        String s2 = "foo";

        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));

        String s3 = new String ("foo");
        System.out.println(s1 == s3);
        System.out.println(s1.equals(s3));
    }
}

which outputs:

true
true
false
true

I am trying to understand and explain what's going on here. And I need some guidance. Will I be right to say s1 and s2 are same objects stored in different parts of memory and that's why we get the first 2 trues?

Why is the 3rd output false? is s3 a different object? and why does it produce true with the equals method on s1? I'd appreciate some help. Thanks :).

EDIT: This question is not about == nor equals() I know what they are. My question is more related to memory references and addresses. So don't just presume I'm asking about String Comparison. I'm asking about a whole different thing here.

emi
  • 2,830
  • 5
  • 31
  • 53
  • 1
    I don' think this question is about String comparison, but String object reference comparison – Paul Samsotha Nov 19 '13 at 04:51
  • This link will clear all your doubts. http://www.thejavageek.com/2013/07/27/string-comparison-with-equals-and-assignment-operator/ – Prasad Kharkar Nov 19 '13 at 04:56
  • 2
    I don't know why people closed this question. It's not a "how do I compare strings" question. – Paul Samsotha Nov 19 '13 at 04:59
  • @peeskillet exactly! this is not the question they are thinking it is. they just read the subject and assume something else cause it' a common quesion. but i mean something completely different here. – emi Nov 19 '13 at 13:16
  • @siaw_23 I saw this exact same type question asked not to too long ago, and it got a bunch of upvotes. No closes for duplicate "How do I compare Strings?" – Paul Samsotha Nov 19 '13 at 13:30

4 Answers4

1

That's because in the 3rd scenario, a new object is created(the new keyword creates a new string object) and == is used for object reference equality. That's why it returns false.

Whereas the equals() method is used for String value equality, and hence, it returns true.

In the first and second case, the "foo" String literal is interned and thus, both s1 and s2 refer the same String literal.

Rahul
  • 44,383
  • 11
  • 84
  • 103
1

The == operator checks reference equivalence. If the two references are equivalent, then they're the same instance.

Your first example works because the strings are interned - but don't rely on this. equals() actually checks to see if the two Strings contain the same data.

With the new keyword, a new reference is created for the other string - hence, they no longer point to the same instance.

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

Because == tests that the strings refer to the same object and equals tests if they are equivalent. Your line

String s3 = new String ("foo");

constructs a new reference to the constant "foo" String. Take a look at What is String interning? for a bit more, but be aware that interning will use PermGen space.

Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

String are put into a String pool

When you have

String s1 = "foo";
String s2 = "foo";

Only one "foo" is placed into the String pool.

So

s1 == s2 is true becuase they reference the Same String "foo" in the pool

To answer your question, your assumptions are right!

The last true s1.equals(s3) compares the value "foo", that's why they are equal.

So just remember with Strings

== compares Object reference

equals compares value.
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720