0

Here I was training to my OCP and test some String behaviors.

And when i get this:

    System.out.println("a".substring(0) == "a".substring(0));  // true

    System.out.println("a".substring(0) == "aa".substring(1)); // false

Some doubts came to me. I know what that method returns is:

    return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);

But, theres a conceptual error on this compiler rules or thats all ok?

Akhil Jain
  • 13,872
  • 15
  • 57
  • 93

2 Answers2

2

In the first case substring returns this, and this == this returns true as expected.
In the second case, substring returns a new object so == returns false because you are comparing different objects.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • @user3220398 why what? Why is substring implemented that way? – assylias May 07 '14 at 18:42
  • @user3220398 if `beginIndex` is 0 then the result of `substring` is the original string, so instead of creating a new string, substring just returns `this`. That is more efficient CPU and memory wise. I'm not sure I understand why you think this is an issue. – assylias May 07 '14 at 18:48
  • About performance it's all right. About the concept, that's what i mean, if user don't know the implementation of language, this behavior has an exception , a single case isolated. So by the way, the static context "this" not always pointing to the same memory heap, ok? – Guilherme Ribeiro Dev May 07 '14 at 18:57
  • @user3220398 `substring` does not guarantee anything about returning the same or a different string instance. So the current implementation is fine and returning a new string would be fine too. – assylias May 07 '14 at 19:01
  • @assylias Interestingly enough, the javadocs for `subString()` say that a new String is returned... So it seems that returning `this` could be just a performance optimization since you aren't supposed to assume that you get the same string back. – awksp May 07 '14 at 19:16
  • I see - it is indeed a bit ambiguous. You should have phrased your question that way! – assylias May 07 '14 at 19:32
1

You're using the wrong equality operator. You want .equals, not ==. Whether or not the strings returned by the method calls you're using are the same string objects is an implementation detail; you want .equals to compare their contents.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • To a pratical effect you are right. But in this case, i was expect that operator "==" ever returns false with substring() method, knowing that it return a String instance with "new" operator. I just don't understand why only in case of the parameter beginIndex == 0. Why return static context with "this" instead new String(this.value) for example? – Guilherme Ribeiro Dev May 07 '14 at 18:32
  • @user3220398 `s.substring(0)` just returns `s`, so the runtime **could** just return the same reference as `s`. That doesn't mean it necessarily will. You can't count on it. But it might. That's one reason **never** to use `==` with `String`s even if you think you can tell when it will return a different reference. – ajb May 07 '14 at 18:39
  • @ajb I've been satisfied now. I know however was implemented, the method runs fine with the right use. I **might** use "a" == "a" , but I'm sure that is true because at that runtime this==this. But i'll never use it. – Guilherme Ribeiro Dev May 07 '14 at 19:11