Possible Duplicate:
How do I compare strings in Java?
I have this code:
System.out.println(staff.getEmail().length());
System.out.println(staff.getEmailList());
if ((staff.getEmail().length() > 0) && (staff.getEmailList() == "Y")) {
System.out.println(staff.getEmail());
This is the output on my eclipse console:
12
Y
My problem is that even though both conditions in the if statement are satisfied, my program can never get to the last line of code above (System.out.println(staff.getEmail())). I don’t any error too.
Any ideas what could I’ve done wrong? thanks 🙂
You should use
String.equals()and not operator == for string equality in java.staff.getEmailList() == "Y"should be converted into"Y".equals(staff.getEmailList())The reason for it is
operator==checks for identity. If the two operands are the same object (referencing to the same object), whileequals()checks for equality – if the two operands equal each other.