Quote:
Originally Posted by Ethan_P
You have (door == "t"), this is not recommended.
Strings are objects, unlike ints and other primitive types. And because Strings are objects, you can't use '==' to compare two Strings. You would need to use
string1.equals(string2);
|
It might be useful to talk about the reason for this.
Let's say I have two Strings:
String a = "blah";
String b = "blah";
When I instantiate these Strings, Java allocates two different memory locations for them. And 'a' points to the first one that has the "blah" data in it, and 'b" points to another location with the "blah" data in it.
That's why a==b returns false, because 'a' and 'b' are pointing to two different objects, even if they have the same value.
The '==' operator in this case checks to see if they are the same object, not if the have 'equal' values.