-3

In Java I have written below code for string trim() method.

           if(" String ".trim() == "String")
               System.out.println("Equal");
           else
               System.out.println("Not Equal");

It gives output Not Equals which I understood because " String ".trim() had returned new String object reference .

But when I trim and compare without white spaces it gives output Equals.

            if("String".trim() == "String")
               System.out.println("Equal");
           else
              System.out.println("Not Equal");

If String is not having white spaces what trim() method returns? I know equals() I can use but in my exam I got this question.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sumeet Kumar Yadav
  • 11,912
  • 6
  • 43
  • 80

3 Answers3

5

Did you look at the API? This is clearly stated in documentation of trim() method:

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.

You could have also gone through the source code. The trim() method returns this, if there are no whitespaces removed at either ends:

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

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

So, if both st > 0 and len < value.length are false, which would be case for strings that don't have whitespaces at either ends, it returns this.

Now, why "String" == "String" is true is by the virtue of String interning. You can get lot more information about this through google.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Alternatively, the API (http://docs.oracle.com/javase/7/docs/api/index.html) already states: "Returns a copy of the string, with leading and trailing whitespace omitted." So if there is no whitespace to omit, nothing is changed. – SirRichie Jan 11 '14 at 13:16
  • @SirRichie However the API doesn't make it clear whether a new copy is created or not for no whitespaces at the ends. – Rohit Jain Jan 11 '14 at 13:17
  • @SirRichie Oh! Actually the API makes it clear. Sorry for the misleading comment. I'll add the statement from API to the answer. – Rohit Jain Jan 11 '14 at 13:20
4

If the trim() method would do nothing (because the string is already trimmed), the same string object (ie this) is returned.

String interning means that the string constant "String" is the exact same object used throughout the code wherever "String" is used.

These two facts together is why == is true for the comparison of "String".trim() == "String".

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

The == operator checks if it is the same object. To compare equality of the object, use .equals() method. In your second case, the String which doesnt have whitespaces will no do anything to the Object. Hence it is equal, since the object is the same.

Hrishikesh
  • 2,033
  • 1
  • 16
  • 26