This site requires JavaScript, please enable it in your browser!
Greenfoot back
darkmist255
darkmist255 wrote ...

2011/11/24

&& operator with strings?

darkmist255 darkmist255

2011/11/24

#
Java says "bad operand types for binary operator '&&' " when I do
        if(Xdirection = "right" && Ydirection = "down")
        {
        setLocation((getX() + (int)Xmomentum), (getY() + (int)Ymomentum));
        }
Can strings not be used with the && operator? I would think that conditional operators would still work with strings? If not, should I just use integers to represent the directions?
kiarocks kiarocks

2011/11/24

#
when checking for equality, use "==", not "=".
darkmist255 darkmist255

2011/11/24

#
Argh, why didn't I notice that? Thanks :D.
AwesomeNameGuy AwesomeNameGuy

2011/11/24

#
Kiarocks is right, but he left out an important point. Strings are objects. Every object inherents a method called equals. When seeing if two objects equal eachother, you should use this method. This method can be overridden for objects you create. Convinently, strings already have this method overridden to work with them. So to see if strings let's say call them s1 and s2 are equal you use if (s1.equals(s2)). if (s1.equals("some string")) works too. If you use the equality operator == like this if (s1 == s2) the program will check and see if s1 and s2 point to the same string, that is, it will check if the references s1 and s2 are equal to each other, not if the strings that s1 and s2 point to are equal. But if you use the equals method if (s1.equals(s2)) it checks to see if the string objects they point to are the same as each other. See the difference?
mjrb4 mjrb4

2011/11/24

#
All the above boils down to is never use == when comparing strings, always use .equals(). So your original code would become:
if("right".equals(Xdirection) && "down".equals(Ydirection))
davmac davmac

2011/11/24

#
AwesomeNameGuy is right. Unfortunately, comparing strings with '==' seems to work a lot of the time, but there are cases when it won't! It's always best to use .equals() to compare strings rather than '=='.
kiarocks kiarocks

2011/11/24

#
Right, I forgot about that.
darkmist255 darkmist255

2011/11/24

#
Very interesting. My code works with "==", but I will change it for the purpose of practice since it's not really proper. Thanks :D.
AwesomeNameGuy AwesomeNameGuy

2011/11/24

#
I think that == seems to work sometimes because Java handles strings a little differently than most objects, it has something called a "String Literal Pool" where it has all the strings that are coded into your program already sitting there in memory, or something like that, and it is optimized to use as few strings as possible, so if you have literal strings in your code, and you reference them, a lot of the time you will be referencing the same string anyway and == works. But other times it won't work. Just remeber the difference between references, primitives, and objects. == compares references and primitives, .equals() compares objects. And you have to override .equals() if you are comparing your own objects.
You need to login to post a reply.