I have the following Java code:
String p = "seven";
String q = "teen";
p + q == "seventeen";
Why does the last line return false and not true?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because you should use the
equalsmethod forStringcomparison. The==operator compares the object references, and those are unique for each object. You would only gettruefor a==comparison when comparing an object to itself.Try
(p + q).equals("seventeen");Note, that
Stringcomparison in java is case-sensitive, so you might also want to take a look at theequalsIgnoreCasemethod.