Avatar of me_patrick
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.

Java

Avatar of undefined
Last Comment
vahana

8/22/2022 - Mon
Mick Barry

== tests for object equality
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)

CEHJ

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


sciuriware

if( "bingo" == password)

NEVER DO THAT:

if(password.equals("bingo"))
{
..................

See why?

;JOOP!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
Mick Barry

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sciuriware

>>> 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!
sciuriware

O what are you guys quick this day.
I was still typing when ........................


;JOOP!
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
me_patrick

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sciuriware

.............................. and JOOP is my first name.

;JOOP!
ASKER
me_patrick

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.
CEHJ

me_patrick: out of interest, from which application did you post the code at http:#23496305 ?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
me_patrick

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
CEHJ

Thanks. The quotes are broken in my browser for this site. They're fine from the original
vahana

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.