Quote:
Originally Posted by alireza6485
Hi,
my friend said I should change while(again=="yes") to again.equals("yes")
Why should I do that?Why does not java like again=="yes"???
Your friend is right.
The .equals() does something well documented: it returns true if and only if the two strings are made up of the same characters in the same order. This is exactly what you want and so you should use that method.
== does something different. It is true if and only if the two references it is comparing have the same value. References have the same value when they refer (point to) the same object. In the case of strings it should be clear that you can have
different strings made up of the same characters in the same order. (As an example look at the 5th and the 15th words in the previous sentence. One is near the start of the sentence, one is in the middle. They are clearly not "the same" in the sense of identical. But they are made up of the same characters in the same order.).
An expression like
new String("whatever"); will always return a reference to a string that is not identical to any others. (That's what "new" means). But the string it refers (points) to may well be equal() to others.