2

Possible Duplicate:
String comparison and String interning in Java

I have small doubt regarding String comparisons in Java, consider the following code:

if("String".replace('t','T') == "String".replace('t','T')) {
  System.out.println("true");
}
else {
  System.out.println("false");
}

The above code always print's false, where as if I try like this:

if("STring" == "STring") {
  System.out.println("true");
}
else {
  System.out.println("false");
}

It will always print me true. Yes, I know String comparisons should be done with String.equals() or equalsIgnoreCase() method. But this is one of the question was asked in interview and I am confused. Can anyone guide me on this behavior?

As per my knowledge, in code snippet 1, "String.replace('t','T') is returning object, so object comparisons returns in false. Am I right?

0

6 Answers 6

5

"String.replace('t','T') is returning object, so object comparisons returns in false. Am I right?

Yes, as for this case, you are right. String#replace(or any method of String class for that matter), will return a new String object (You can guess why? Immutability). And thus you would have to do the comparison using equals method, to compare their contents.

Now, in the second case: -

"STring" == "STring"

You are comparing two string literals. Now, since String literals are interned in Java, so both the literals are same (in the sense, they point to the same memory location), and hence == comparison gives you true.

The difference in comparison using == and equals is that, == compares the reference value - i.e value of memory location of objects, which will be different for two different string objects, as you are having in first case. Whereas, equals compares the actual content in those objects.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Rohit Jain, I said same in interview, and my assumption was correct.. :)
@PradeepSimha.. You're welcome :) So, what was the reply of the interviewer? Sure he must have said something?
nothing, just he said "ok".. :D
4

"String.replace('t','T') is returning object, so object comparisons returns in false. Am I right?

Yes, == compares object references, and your first code is comparing two different objects.

As far as the second code is concerned its due to string interning.

1 Comment

please read through this article, javadude.com/articles/passbyvalue.htm . This would even be an answer to the whole question aswell. There is no pass by reference
1

ok lets do it like this, your both String objects "String" are referering to the same object. So they are "basicly" equal. That is a thing the compiler does for you

but the method replace, does create and return a new String object, and that is why your second code is not equal.

Comments

0

Java always compares the basic types (int, byte, etc) or references for objects when using ==.

The java compiler optimizes the two string constants you entered to use the same object, thus the same reference, thus the == return true

Comments

-1

DO this way

("String".replace('t','T').Tostring() ==  ("String".replace('t','T')).ToString()

This will solve your problem because the replace statement should be converted to string before eveluation.

You can also user the String.Equals for this or better you use ignore case as you mention in your question.

Comments

-1

Try this:

if(string1.equals(string2)){

...

}

1 Comment

Yes, but my doubt was "why it is returning false"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.