Urgh.  Why doesn't "=" work.  

Ok, classic n00b question:

The following snippet of code is from a socket based text game (yeah, yeah, ANOTHER mud…) I can’t make the server understand the input from the user. I can replace the entire if statement with out.println(Command) and it will echo whatever you type to it, but if you try to perform an operation on the Command (compare it to say “q”, quitting if it matches) it simply ignores the input. What am I doing wrong?

while((Command = in.readLine()) != null){

       if(Command == "q"){
       [b]do something[/b]
       };
    
             
             } 

Regards, ???

String bla=“moo”;
String foo=“moo”;
String bar=“MoO”;

if(bla.equals(“moo”)) //true

if(foo.equals(bla)) //also true

if(foo.equals(bar.toLowerCase())) //also true

if(“moo”.equals(bar)) //not true

O_O

A straight, intelligible, non-condescending answer in a web forum which answered specifically my question.

Woah.

Thankyou so much, I’m gonna stick around here.

A String is an object.
The == operator checks if the objects are the same object (it basically compares the references of the objects, so comparing 2 strings that way becomes a simple integer comparison). With Strings, you may get a result you won’t expect when you do:

if (stringA == stringB) {

}

when both stringA and stringB hold “HelloWorld”

This is because stringA and stringB may be different objects, even though the contents are the same.
Using == on Strings will work when you define the Strings literally in your source (ie, String stringA="HelloWorld; String stringB=“HelloWorld”), or when you would do stringA.intern(); stringB.intern(); first (but don’t start doing the latter now without knowing what it actually does).

If you want to learn more about this, check the source from the String class, and more specifically String.intern()

Hope this helps:

http://java.sun.com/docs/books/tutorial/java/index.html

It sure helped me back in the day…