Wow, that's even more stupid.
That's because, despite all the magic and hand-waving, Strings are still objects, and Java tests equality of objects by seeing if they exist at the same place in memory (like C/C++ pointers). Stargrasper, unless that was changed in Java 6, Max is correct.
No, actually it makes perfect sense. Java is consistent in its ways of comparing equality of objects. It could be argued that the problem comes from the hand-waving used to make Strings seem like primitives, but that's a different discussion.
The alternative to this system would be to compare all objects and primitives based on value, and incorporate pointers into the language. As a C/C++ programmer, I know exactly how badly pointers can be used. Plus, I don't think there's a viable way to even add that functionality to Java without re-writing 70% of the language. The way things work now is the lesser of the two evils.
@Stargrasper: From what you've said, it sounds like that was a change made in Java 6. Try it in Java 5. I'll put a screenshot of my program up in a second. Also, try comparing two String objects.
EDIT: I remembered now that it was comparing two String objects that doesn't work. I've never tried comparing a String object to a literal because of this.
EDIT 2: Why is it working in this class but not in the other class?
// A program from my compsci class
import java.util.Scanner;
public class P4_bad {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Do you want to continue?");
String response = input.nextLine();
if(response == "y" || response == "yes" || response == "ok" || response == "sure" || response == "why not?") {
System.out.println("OK");
} else if(response == "n" || response == "no") {
System.out.println("Terminating");
} else {
System.out.println("Bad input: " + response);
}
}
}
Do you want to continue?
n
Bad input: n