1

Possible Duplicates:
Getting strange output when printing result of a string comparison

Hi all,

System.out.println() behaving in a different way with strings. Can any one explain why

See the below code snippet

String a ="hello"
String b ="hello"

System.out.println("a==b"+"is"+a==b)

I expect this to print 'a==b is true', but it just prints false and I dont know why.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
mahati
  • 209
  • 5
  • 10
  • in fact, most modern java compilers pool string literals, so it might actually print `a==b is true`, although that's pure luck, and as everyone mentioned, `.equals()` is how you're supposed to compare – davin Feb 05 '11 at 10:01
  • 3
    @davin: no, his example just prints `false` because the `a` is being concatenated to `"a==b"+"is"` and that is being compared to `b` using `==`. – Bart Kiers Feb 05 '11 at 10:03
  • @Bart, the question didn't look like that when I commented. Your comment and mine are obviously not synchronised, I will gracefully kill my thread ('thread' in the programming sense, not the forum sense), having little interest in the question, I was merely pointing out a side fact although with all those edits, it's no longer relevant. – davin Feb 05 '11 at 10:09
  • @davin, a JVM is [required by the specification](http://java.sun.com/docs/books/jvms/second_edition/html/ConstantPool.doc.html#73272) to produce identical objects for identical string literals (and only literals), so in fact Bart's answer is the only correct one here. – Sergei Tachenov Feb 05 '11 at 10:10
  • @Sergey, did you read what I wrote? The question's content changed. I wasn't commenting about the answers... – davin Feb 05 '11 at 10:17
  • 1
    Not a duplicate. This is about operator precedence, not string comparison. – finnw Feb 05 '11 at 11:55
  • @davin, I only saw your comment after I had posted mine. Still, my comment has nothing to do with the question's content. I was talking about the "pure luck" part about your comment. That's not pure luck. Identical string literals are required to be represented by the same object. It's not implementation-specific. If they are not literals (like in the current version of the question), that's a different story. – Sergei Tachenov Feb 05 '11 at 13:20
  • @Sergey, please, do me a favour, do you really think I was suggesting that the compiler's decisions are literally "pure luck"? I was referring to the fact that regarding the outcome of a==b where a,b are Strings is almost always false, even if a.equals(b), although in the case that was then the current question it happened to be true, which was "lucky" because the OP said he expected true. Don't nitpick. – davin Feb 05 '11 at 13:40
  • @david, oh, I see, you were talking about general case. I got confused by the phrase "most modern java compilers pool string literals". – Sergei Tachenov Feb 05 '11 at 14:30
  • 1
    Sorry, I voted to close before realizing it wasn't about string comparison but the fact that `a` was concatenating before it was compared to `b`. Voted to re-open. – Bart Kiers Feb 05 '11 at 16:16

3 Answers3

8

A single false is printed because you didn't group your boolean expression.

The expression:

"a==b"+"is"+a==b

is evaluated as

("a==b"+"is"+a) == (b)

while you wanted it to do a string concatenation:

"a==b"+"is"+ (a==b)

That said, you shouldn't compare strings using ==, as others pointed out.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
1

problem is not with System.out.println(); it is something with String objects comparison with ==. Use .equals() method for comparing two string objects. Refer the below link. String Object comparison

R K
  • 1,283
  • 1
  • 14
  • 41
  • *"with System.out.println(); it is something with String objects comparison with `==` Use `.equals()`"* -- Could you provide a link that supports you claim? If so, I'll remove my -1, but I'm pretty sure it's incorrect. – Bart Kiers Feb 05 '11 at 13:57
  • String object's reference will be compared if we use == !!! I didnt mention == comparison has a problem, i mentioned using == leads to a problem. We might not get the actual result if we use == for String comparison. – R K Feb 07 '11 at 04:58
-1

It might be because it is traeting a and b as diffrent objects. When we create Strings like this it refers to diffrent string pools.

Chirag Tayal
  • 459
  • 1
  • 6
  • 14