[Java Silver] About equals method

Here's a summary of the equals methods you should remember when studying Java Silver.

What is the equals method?

equals method

The equals method is a method defined in ** java.lang.Object class **.

Normal behavior


Check if the instance stored in variable a and the instance stored in variable b have ** the same value ** ** (equivalence) **

String a = "abc";
String b = new String("abc");

System.out.println(a == b); //false is returned
System.out.println(a.intern() == b.intern()); //returns true

This is because the intern method is a method for finding and reusing strings in memory, including constant pools. A constant pool means that when a ** character literal ** reappears in a program, ** references to the same string instance created in the past are reused **.

Equals method of Object class

Since the equals method is defined in the Object class, every class has it. However, only the equals method of the ** Object class ** is defined to determine ** identity ** rather than equivalence. The definition is as follows.

public boolean equals(Object obj){
   return (this == obj);

Since the ** equivalence confirmation method ** differs for each class, it is assumed that the equals method is ** overridden and used **.

That it is a method defined in java.lang.Object class

Override the method defined in ** java.lang.Object class ** as ** default method ** in ** interface ** → ** Compile error **

Therefore, when overriding the equals method, it must be overridden in ** class **.

** * List of methods defined in java.lang.Object class ** スクリーンショット 2020-10-04 18.36.43 1.png

NullPointerException does not occur even if null is used as an argument

Passing null to the equals method does not result in a NullPointerException

Official document

x.equals (null) returns false.

From <Sample comparing null or empty string with Java equals>

When a.equals (b), if a is null, a NullPointerException will occur. When a.equals (b), if a is not null and b is null, false is returned. Objects.equals (a, b) added in Java7 does not generate a NullPointerException even if a and b are null.


Sample comparing null or empty string with Java equals

Recommended Posts

[Java Silver] About equals method
About Java method binding
About method splitting (Java)
[Java Silver] About initialization
About inheritance (Java Silver)
Java Silver Study Method Memo
[Java Silver] Array generation method
Java method
Java method
[Java] method
[Java] method
[Java beginner] == operator and equals method
My thoughts on the equals method (Java)
About Java interface
[Java] About Java 12 features
Java Silver exam procedure and learning method
Java Silver memo
[Java] About arrays
[Java] How to compare with equals method
How to use submit method (Java Silver)
Java8 method reference
Where about java
About Java features
About the method
[Java] forEach method
About Java threads
[Java] About interface
About Java class
About Java arrays
About java inheritance
About interface, java interface
java8 method reference
[Java] Random method
Study Java Silver 1
[Java] split method
About List [Java]
About java var
About Java literals
About Java commands
How to use the replace () method (Java Silver)
[Qualification Exam] Java SE 8 Silver Learning Method Summary
About Java log output
Output about the method # 2
About Java functional interface
About No Method Error
Java, about 2D arrays
Passed Java SE8 Silver
About class division (Java)
JAVA DB connection method
java bronze silver passed
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
[Java] About Singleton Class
Java Silver Study Day 1
Java learning memo (method)
Java Silver passing experience
Summarize Java inheritance (Java Silver 8)
[Java ~ Method ~] Study memo (5)
[Java] About anonymous classes
Studying Java 8 (see method)
[java] Java SE 8 Silver Note