0

When I compare ("String"=="String"), what exactly I am comparing, the two objects or two references?

Please explain the output of following code snippets:

1) System.out.println("String".toString()=="String"); // output is true

2) System.out.println("String".trim()=="String"); // output is true

3) System.out.println("String ".trim()=="String"); // output is false

According to statement 1 and 2, the output of 3rd statement should also be true. Please explain what is going on here.

Vikas Mangal
  • 821
  • 3
  • 10
  • 23
  • because `"String "` and `"String"` are two different object...and you are comparing reference. You can not conclude that line *"According to statement 1 and 2..."* – Not a bug Jun 25 '14 at 07:28

6 Answers6

2

From my previous answer, You have to remember, == compares the object references, not the content.

Community
  • 1
  • 1
Linga
  • 10,379
  • 10
  • 52
  • 104
1

trim() returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

  • in the first case the answer is true no surprise about that because the references are equal

  • in the second case there are no leading or trailing spaces so trim returns the same string again the references are equal

  • in the last case trim returns a copy as the string has a trailing white space which results in a different reference so your comparison will be false.

Alexandru Cimpanu
  • 1,029
  • 2
  • 14
  • 38
  • System.out.println("String ".trim()) displays the same hashcode as System.out.println("String"). Then how does the third statement evaluates to false. – HJK Jun 25 '14 at 08:14
0

In java you should never compare strings with the equality operator ==, since that operator compares references rather than contents. The compiler is smart enough to reuse one reference for all equal literals found in the code (so the four occurrences of String in 1) and 2) are probably the same instance). However in 3) you have two different literals and there is no guarantee that "String ".trim() will be represented by the same instances as the literals.

See also: https://stackoverflow.com/a/513839/55787

Use .equals() instead.

Community
  • 1
  • 1
chiccodoro
  • 14,407
  • 19
  • 87
  • 130
0

== compares whether the two String references you are comparing point to same String instance or not.

To answer your question you need to check the source code of methods in String class

public String trim() {
int len = count;
int st = 0;
int off = offset;      /* avoid getfield opcode */
char[] val = value;    /* avoid getfield opcode */

while ((st < len) && (val[off + st] <= ' ')) {
    st++;
}
while ((st < len) && (val[off + len - 1] <= ' ')) {
    len--;
}
return ((st > 0) || (len < count)) ? substring(st, len) : this;
}

public String toString() {
return this;
}

As you can see it return this in case of trim if there is nothing to trim which is why you get true. Also toString simply returns this.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

you have to use the method

 equals() 

to compare Strings. Instead with == you are comparing

references. 
FrankTan
  • 1,626
  • 6
  • 28
  • 63
0

== compares memory location . In the first case , the toString method returns the same object ,and since both "String" objects being compared point to the same object on the constant pool, the result is true. In the second case, the trim method does no modification to the String so it returns the same object, with the same result. The third case and the second call to trim returns a different object, not located on the constant pool

omu_negru
  • 4,642
  • 4
  • 27
  • 38