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?

Community
  • 1
  • 1
Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107

6 Answers6

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
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.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • please read through this article, http://javadude.com/articles/passbyvalue.htm . This would even be an answer to the whole question aswell. There is no pass by reference – SomeJavaGuy Jan 08 '13 at 08:00
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.

SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
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

Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
-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.

Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45
-1

Try this:

if(string1.equals(string2)){

...

}

pidel
  • 65
  • 9