リンクを新しいタブで開く
  1. いいね!
    興味なし

    The equals method in Java is a fundamental part of the Object class, used to compare two objects for equality. By default, the equals method in the Object class performs a shallow comparison, checking if two references point to the same memory location (i.e., == operator). However, this behavior can be overridden to perform deep comparisons based on the object's state.

    Syntax and Default Behavior

    The default implementation of the equals method is:

    public boolean equals(Object obj) {
    return (this == obj);
    }
    コピーしました。

    This checks if the current object (this) and the passed object (obj) refer to the same instance.

    Overriding the equals Method

    To compare objects based on their state (fields), you can override the equals method. Here's an example:

    class Geek {
    private String name;
    private int id;

    public Geek(String name, int id) {
    this.name = name;
    this.id = id;
    }

    @Override
    public boolean equals(Object obj) {
    if (this == obj) return true; // Reflexive
    if (obj == null || obj.getClass() != this.getClass()) return false; // Null and type check
    Geek geek = (Geek) obj;
    return this.name.equals(geek.name) && this.id == geek.id; // Compare fields
    }
    }
    コピーしました。
  1. Java’s Objects.equals () Method Explained - Medium

    2024年9月1日 · Explore Java's Objects.equals () method, a null-safe solution for object comparison. Learn how it prevents NullPointerException and simplifies your code.

  2. Java Object equals () - Programiz

    It is because the String class overrides the equal() method so that the method compares the element of the object. Since the values of obj1 and obj2 are different, the method returns false.

  3. Java Object equals () Method

    By default, the equals() method compares the memory addresses of the objects, meaning two objects are equal if and only if they refer to the same instance. However, this method can be overridden to …

  4. equals () and hashCode () methods in Java - GeeksforGeeks

    2025年7月23日 · Deep Comparison: Suppose a class provides its own implementation of equals () method in order to compare the Objects of that class w.r.t state of the Objects. That means data …

  5. Java `equals` Method: Understanding Object Equality

    2025年11月12日 · In Java, the `equals` method is a crucial part of determining object equality. While the `==` operator can be used to check if two references point to the same object in memory (reference …

  6. 他の人も質問しています
  7. Java | 基礎文法:equals の基礎 | MONO365 -LifeHack-

    2 日前 · equals の全体像 Java の equals は「二つのオブジェクトが“意味として”同じか」を判定するメソッドです。参照が同じか(同一性)は == 、意味が同じか(同値性)は equals で比べます。標準 …

  8. java - What does equals (Object obj) do? - Stack Overflow

    2011年12月1日 · The equals method is used when one wants to know if two objects are equivalent by whatever definition the objects find suitable. For example, for String objects, the equivalence is about …

  9. Object (Java Platform SE 8 ) - Oracle

    The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x …

  10. Difference Between == and equals () in Java - Baeldung

    2025年11月27日 · Understanding how Java evaluates equality is important when working with both primitive values and objects. Different mechanisms apply depending on whether the comparison checks references or the internal state of …