Possible Duplicates:
Java String.equals versus ==
whats the difference between ".equals and =="
public String getName() {
return new String("foobar");
}
if(getName() != "foobar2") {
//Never gets executed, it should, wtf!.
}
if(!getName().equals("foobar2")) {
//This works how it should.
}
So yeah my question is simple.. why doesn't !=
behave the same as !equals()
aka (not Equals
).
I don't see any logicial reason why one should fail, both are the same exact code in my mind, WTH.
Looking at java operators http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
You can clearly see
equality ==
!=
are the equality operators, sure I usually use !=
only on numbers.. but my mind started wandering and why doesn't it work for String
?
EDIT: Here's something that looks more like the actual issue..
for (ClassGen cg : client.getClasses().values()) {
final ConstantPoolGen cp = cg.getConstantPool();
if(cp.lookupInteger(0x11223344) != -1) {
for (Method m : cg.getMethods()) {
System.out.println("lots of class spam");
if(m.getName() != "<init>") continue;
System.out.println("NEVER GETS HERE, 100% SURE IT HAS CONSTRUCTOR LOL");
}
}
}