Class Object

8377 ワード

java.lang

Class Object

  • java.lang.Object
  • public class Object

    Class  Object  is the root of the class hierarchy. Every class has  Object  as a superclass. All objects, including arrays, implement the methods of this class.

    Methods
     
    Modifier and Type
    Method and Description protected Object clone()
    Creates and returns a copy of this object. boolean equals(Object obj)
    Indicates whether some other object is "equal to"this one. protected void finalize()
    Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. Class<?> getClass()
    Returns the runtime class of this  Object . int hashCode()
    Returns a hash code value for the object. void notify()
    Wakes up a single thread that is waiting on this object's monitor. void notifyAll()
    Wakes up all threads that are waiting on this object's monitor. String toString()
    Returns a string representation of the object. void wait()
    Causes the current thread to wait until another thread invokes the 
    notify()  method or the 
    notifyAll()  method for this object. void wait(long timeout)
    Causes the current thread to wait until either another thread invokes the 
    notify()  method or the 
    notifyAll()  method for this object, or a specified amount of time has elapsed. void wait(long timeout, int nanos)
    Causes the current thread to wait until another thread invokes the 
    notify()  method or the 
    notifyAll()  method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
     
    1、
    equals()メソッド:あるオブジェクトが別のオブジェクトと等しいかどうかをテストします.Objectクラスでは、2つのオブジェクトが同じメモリ領域を指しているかどうかを判断します.このテストは、同じ内容のオブジェクトでもメモリ領域が異なるため、あまり役に立ちません.オブジェクトが等しいかどうかをテストするには、このメソッドを上書きして、より意味のある比較を行う必要があります.たとえば
     1 class Employee{
    
     2 ... // 《java 》 
    
     3     public boolean equals(Object otherObj){ // 
    
     4     if(this == otherObj) return true; // null, false
    
     5     if(otherObj == null) reutrn false; //
    
     6     if(getClass() != otherObj.getClass()) return false; // otherObj Employee 
    
     7     Employee other = (Employee)otherObj; // 
    
     8     return name.equals(otherName)
    
     9             && salary == other.salary
    
    10             && hireDay.equals(other.hireDay);
    
    11     }
    
    12 }

     
    Objectクラスのequals()メソッドでは、equals()メソッドを呼び出す参照が転送された参照と一致するかどうか、すなわち、この2つの参照が同じオブジェクトを指しているかどうかを判断します.
    Objectクラスのequals()メソッドは次のとおりです.
    public boolean equals(Object obj)
    
    {
    
        return (this == obj);
    
    }

     
    すなわち,Objectクラスにおけるequals()メソッドは==に等しい.
    継承クラスがequals()メソッドを継承Objectのクラス上書き(override)した後に,equals()メソッドを用いて2つのオブジェクトが等しいかどうかを比較することを実現した場合にのみ,equals()メソッドは==と異なるといえる.
    約定:x.equals(null)は、空でない参照xに対してfalseとして返されます.
    またequals()メソッドを上書きする場合はhashCode()メソッドを同時に上書きすべきであり,逆も同様である.

    int hashCode()


      Returns a hash code value for the object.
    equals()メソッドを上書きした後、hashCode()メソッドも上書きしなければなりません.逆も同様です.
    このメソッドは、2つのオブジェクトがequals()メソッドによって等しいと判断された場合、同じhash codeを持つべき整数値(hash code value)を返します.
    ObjectクラスのhashCode()メソッドは、異なるオブジェクトに対して異なる値を返し、ObjectクラスのhashCode値はオブジェクトのアドレスを表します.
    すなわち,両オブジェクトはequals()法でfalseを返し,それらのhashCodeは同じでも異なってもよい.しかしながら、2つの等しくないオブジェクトに対して2つの異なるhashCodeを生成することは、ハッシュテーブルの性能を改善することができることを認識すべきである.