0
Why aren´t these strings evaluated as equal?
Hello everyone. I am writing a program in Java and hit one problem. I need to check user-inputed password (string) with password stored in a Player object. The Player´s method looks like this: boolean checkPassword(String password){return (password==this.Password);} When a user type correct password, it should return true. However this code alway return false. It looks like Java always evalute two string inputs or string input and string defined in programm as not equal. Can you tell me what should I do?
3 Answers
+ 3
Use
a.equals(b)
c.equals(d)
== operator is used to compare primitive types like ints, floats, chars ect. The equals method can be overridden to achieve the same effect.
+ 1
Here is a example where you can see it clearly:
https://code.sololearn.com/cbpS6AJ70Ovy/#java
0
Thank you a lot.