■ == Operator: Operands (values and variables) to compare ■ equals method of object class: What to compare is an object
● The equals method is supposed to be overridden.
[Equals method of String class]
In the String type equals method, the String type character string is assigned to the char type array and checked character by character. That is, an instance of String type is judged as a character string.
(1) String a = new String("sample"); (2) String b = "sample"; (3) System.out.print(a==b); (4) System.out.print(a.equals(b));
[Explanation] (1) String type instance (2) String type literal (3) Execution result: false Reason: Because the reference is different (4) Execution result: true Reason: Because the character strings are being compared
Recommended Posts