me_patrick
asked on
Simple Java question
Why does ..
String password = "bingo";
if ( args[0] == password) not work yet
if( "bingo" == password) does?
PS I am entering the word "bingo" in as a run time parameter
PPS I know I can use args[0].equals(password), I'm just curious why '==' doesn't work in this case.
String password = "bingo";
if ( args[0] == password) not work yet
if( "bingo" == password) does?
PS I am entering the word "bingo" in as a run time parameter
PPS I know I can use args[0].equals(password), I'm just curious why '==' doesn't work in this case.
That will be because (when it works) the string is drawn from the constant pool and is the same in each case. i.e. it has the same reference. So both
password.equals("bingo")
and
password == "bingo"
are true
password.equals("bingo")
and
password == "bingo"
are true
if( "bingo" == password)
NEVER DO THAT:
if(password.equals("bingo" ))
{
..................
See why?
;JOOP!
NEVER DO THAT:
if(password.equals("bingo"
{
..................
See why?
;JOOP!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>> if( "bingo" == password) :::
::: "IF THE MEMORY ADDRESS OF THIS LITERAL IS THE SAME AS THAT OF 'password'".
But, you want to know if the content of password is equal to "bingo".
;JOOP!
::: "IF THE MEMORY ADDRESS OF THIS LITERAL IS THE SAME AS THAT OF 'password'".
But, you want to know if the content of password is equal to "bingo".
;JOOP!
O what are you guys quick this day.
I was still typing when ........................
;JOOP!
I was still typing when ........................
;JOOP!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wow ... thanks for all the responses. Let me see if I'm getting this ...
String a = "abc", b="abc", c="123";
.... each of these variables are all part of this particular instance of the String Class therefor they would all evaluate as true when compared with ('==') each other .... is that correct?
Secondly, on reading Jim's blog...
final String s1 = j;
final String s2 = im;
String jim = jim;
System.out.println(s1+s2 == jim); // returns true: Constant expression.
String s3 = j;
String s4 = im;
System.out.println(s3 + s4 == jim); // returns false: Not a constant expression.
Is this just very subtle or is it a typo - these comparisons look identical to me, what am I missing?
Thanks again.
Patrick.
PS I'm not up on these texting shortened phrases ... what does ;JOOP! mean?
String a = "abc", b="abc", c="123";
.... each of these variables are all part of this particular instance of the String Class therefor they would all evaluate as true when compared with ('==') each other .... is that correct?
Secondly, on reading Jim's blog...
final String s1 = j;
final String s2 = im;
String jim = jim;
System.out.println(s1+s2 == jim); // returns true: Constant expression.
String s3 = j;
String s4 = im;
System.out.println(s3 + s4 == jim); // returns false: Not a constant expression.
Is this just very subtle or is it a typo - these comparisons look identical to me, what am I missing?
Thanks again.
Patrick.
PS I'm not up on these texting shortened phrases ... what does ;JOOP! mean?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
.......................... .... and JOOP is my first name.
;JOOP!
;JOOP!
ASKER
Thanks guys for your contributions - I'm finding this quite tricky to get my head around but you given me enough ideas to go away and have a think.
PS. Sorry about the name blunder Joop.
PS. Sorry about the name blunder Joop.
me_patrick: out of interest, from which application did you post the code at http:#23496305 ?
ASKER
Do you mean ...
final String s1 = j;
final String s2 = im;....... etc?
That was cut and pasted from the Jim Blog article you suggested.
Regards.
Patrick
final String s1 = j;
final String s2 = im;....... etc?
That was cut and pasted from the Jim Blog article you suggested.
Regards.
Patrick
Thanks. The quotes are broken in my browser for this site. They're fine from the original
Very good link , When u pass string as args it create new String and add it to String array , I think that why
"==" operator compare to different obj
"==" operator compare to different obj
you needs to equals() method to test object equality
the fact that bingo works is just coincidence that that are the same object (due to string pooling)