The String class replace(char, char) method scans for a matching character. If it does not find it, it returns an instance of the String host as is. Naturally, comparing the returned link to the original link using the == operator returns true, since this is a test reference equality and the two links are the same.
If, however, the original string contains the character "H" - using your example here - then the returned string will not be the same as the original string (although it will be the same for each character); it will be a freshly distributed instance that could not be compared by reference equality (again, the == operator). Comparing the returned String with the original via Object#equals() will return true, since the two strings are equivalent, but they will be different instances that cannot match using reference equality.
In contrast, String#replace(CharSequence, CharSequence) treats the target string as a regular expression; it uses Matcher#replaceAll() inside to replace matches in the target template with the following replacement sequence.
Now the question is whether the original string or the just selected copy of Matcher#replaceAll() will return, even if the template does not match. By reading the code in the Oracle library, if Matcher does not find the corresponding template, it returns CharSequence#toString() in the original CharSequence , which for String objects simply returns this link is not damaged. This makes me wonder if you are reporting the true result here.
One obvious hole in the question aStr is the original String content referenced by aStr . You probably wanted to show an ad like
final String aStr = "Hello";
but you didnβt. The result of both expressions should depend on whether aStr contains the character βHβ or not. Assuming this is the case, I expect both expressions to be false, assuming no string interning plays in one of the String#replace() overloads. We know that string literals are interned, but the return values ββthat we see are built in the Oracle library implementation of both String#replace() methods, have just been allocated and not extracted from the internal pool.
source share