-5

Possible Duplicate:
String is not equal to string?
What makes reference comparison (==) work for some strings in Java?

can some one explain me following java code

String a = "1";
if(a == "1") {
//print compare 1 is true;
} else { 
//print compare 1 is false;
}

if(a.equals("1")) {
//print compare 2 is true;
} else { 
//print compare 2 is false;
}

it results like

compare 1 is false
compare 2 is true

Only explanation i have is that its comparing the memory address not the values itself. But i am not sure. can some please put a light on it. in .Net == operator is overloaded to compare contents of string.

Community
  • 1
  • 1
Mubashar
  • 12,300
  • 11
  • 66
  • 95
  • 1
    == is does not work when comparing strings, even though it does not create an error. This is true for any *object* in Java. Using *.equals* will give you a proper result. It is simple as that. – Name Dec 24 '12 at 03:48
  • 7
    I still say we need a "That's not how you compare Strings in Java" close option. – Brian Roach Dec 24 '12 at 03:49
  • 2
    I dont understand what the down voting is about. – Marlon Dec 24 '12 at 03:52
  • 2
    @Marlon - *Research effort*, which if you hover over the downvote button you'll see is the first reason to hit it. This has literally been answered hundreds of times on SO and thousands of times on the internet. – Brian Roach Dec 24 '12 at 03:53
  • 2
    At least he didn't actually try this code, because `if (a == "1")` would actually return `true`, and he'd go on thinking that's how you compare Stings. – Brian Roach Dec 24 '12 at 03:54
  • @Brian Roach: may be you are using some different jdk but i am having a working code, i am not doing any assignment i am fixing some java code and found this issue in my code. thats why i put the question. please avoid being judgmental. – Mubashar Dec 24 '12 at 03:56
  • I'm not being judgmental, I'm telling you how the JVM works (All of them, per the spec), which if you read the answers to the questions this is a duplicate of, all will become clear. If yours doesn't it's literally broken and not to spec. – Brian Roach Dec 24 '12 at 03:58
  • 1
    @Mubashar: Brian is correct. Any compliant JVM wouldn't give the output you've shown. For details, see the [language spec, section 3.10.5](http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5), about "interning" of Strings. – Ryan Stewart Dec 24 '12 at 04:01
  • I am using JDK 6 with "Eclipse Java EE IDE for Web Developers" on windows 7 box. i am getting still same results by executing code in debug mode. Can you guys help me to know why i am not getting as expected. and @Brian: I confess for not putting effort in research before creating question. i would be careful next time. – Mubashar Dec 27 '12 at 00:18

2 Answers2

3

use "1".equals(a) , String is an object so use equals() to compare

jmj
  • 237,923
  • 42
  • 401
  • 438
0

I understood that == operator is compare "Is it same object?"

object a is not same object with constant string "1".

so returns false

Minkyu Kim
  • 1,144
  • 3
  • 18
  • 43
  • 1
    It actually is the same object and will actually evaluate to `true` unless your JVM is broken and not to spec. String literals are interned which is the reason this question gets asked over and over. – Brian Roach Dec 24 '12 at 05:10
  • Thanks to correct my answer. My misunderstood. – Minkyu Kim Dec 24 '12 at 05:43