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?

11th May 2018, 7:14 PM
Jan Štěch
Jan Štěch - avatar
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.
11th May 2018, 8:23 PM
TurtleShell
TurtleShell - avatar
+ 1
Here is a example where you can see it clearly: https://code.sololearn.com/cbpS6AJ70Ovy/#java
11th May 2018, 7:24 PM
Jan Štěch
Jan Štěch - avatar
0
Thank you a lot.
12th May 2018, 11:46 AM
Jan Štěch
Jan Štěch - avatar