Share
Explore BrainMass

Why aren't relational operator (==) and the .equals method interchangeable?

Why aren't the relational operator (==) and the .equals method interchangeable? Why isn't the following code correct?

String myName = "Ken";
if (myName == "Ken")
//do something

Solution Preview

The "==" operator and the .equals method are not interchangeable.
"==" operator is to compare the references of two variables, .equals method is to compare the value of the two objects.

In your example, ...

Solution Summary

This response provides a tutorial correcting the relational operation and the .equals method.

$2.19