Java – the same string comparison gives me false

See English answers > How do I compare strings in Java? 23

String temp = ""+(num1*num2);
Boolean equal = temp == answers[i];

if(equal) {
    correct[i] = true;
    num_correct ++;
}else{
    correct[i] = false;
}

I debugged every detail of the program again, and I'm sure these strings are symbolic Why does Java return false when comparing?

Solution

When using the = = operator with objects in Java, you try to compare object references That is, whether this object handle points to the same act object as other object handles This does not work unless the string is interned

Use string instead equals(Object):

Boolean equal = temp.equals(answers[i]);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>