Strings in Java: equals vs ==

Possible duplicate:
How to compare strings in Java?

  String s1 = "andrei";
  String s2 = "andrei";

  String s3 = s2.toString();

  System.out.println((s1==s2) + " " + (s2==s3));

Providing the following code, why is the second comparison s2 == s3 true? What does s2.toString () actually return? Where is it really located (s2.toString())?

+5
source share
15 answers

First of all String.toStringis no-op:

/**
 * This object (which is already a string!) is itself returned.
 *
 * @return  the string itself.
 */
public String toString() {
    return this;
}

Secondly, String constants are interned, so s1 and s2 are behind the scenes that were changed as the same String instance.

+7
source

String.intern() , , . intern ed, s1 s2 . String.toString() , a.toString() a, a . , s2 == s3.

, , equals(). , , , . , . , , intern ed ( .)

, . , , , , .

+7

, new String("something"), Java .

="something", , , JVM. , , , , , , .

+7

.equals, == java.

== s2.ToString() s2.

java /:

In Sun’s JVM, the interned Strings (which includes String literals) are

perm gen, JVM . intered .

+3

java:

, , , , "", , String.intern.

"andrei" , , "". String s1, s2 String, "andrei".

(s1 == s2) && (s1.equals(s2)) - true. String#toString() String ( String), this. s2 == s3 true.

+3

?

== .

, , '==',

. ==, .

+2

, == , s2 == s3 , s2.toString() s2.

+1

String , toString - String - .

, rt.jar :

public String toString() {
return this;
}

, , s3, , s2.

s1==s2, intern() . , s2 s2=s1, , no?

+1

, , new. == , , .

, , , java-, javac, . . , String.

String s2 = new String("toto");
String s3 = new String("toto");
System.out.println(s2==s3); //yields false!!

- .equals(). Mystring:

class MyString{

    private String s;

    public MyString (String s){
        this.s = s;
    }

    public String getContent(){
        return s;
    }

    @Override
    public boolean equals(Object other){
        if(other instanceof MyString){
            MyString compareTo = (MyString) other;
            return this.s.equals(compareTo.getContent());
        }
        return false;
    }
}
+1

(String.intern()) == , equals.

MyString, java String, s s1 , false.

+1

MyString , , , == false.

String , , String, - (.. String s = "foo";), "foo" , "foo" . (.. String s = "foo"; String otherS = "foo";), otherS "foo" .

String pooling.

+1

s == s1 MyString. ,

enter image description here

s s1 - , toto.

- , :

MyString s = new MyString("toto");
MyString s1 = new MyString("toto");

s == s1 false. true, . :

MyString s = new MyString("toto");
MyString s1 = s;

enter image description here

+1

"==" - . Equals() Object . equals() .

0

== , . , == true , :

String s1 = "...";
String s2 = s1;    // reference assignment!

s1 == s2. == true, .

To compare the contents of two lines, use equals():

if (s1.equals(s2)) {
   ...
}
0
source

s2.toString () returns a string representation. Since it is already a string, it returns itself (so the comparison is true).

All lines are allocated on the heap, the coparison operator simply compares whether they are the same object (which is why s1! = S2).

-2
source

All Articles