看下Api文檔的一些說明java
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.程序員
Since:面試
JDK1.0 express
從JDK1.0就已經存在的元老類,類結構的根,全部類的父類,全部類都實現了這個類的方法,包含arrays。特別強調了arrays。編程
Constructor Summaryapp
Constructors Constructor and Description 編程語言
Object() ide
構造方法介紹。性能
Modifier and Type Method and Description flex
protected Object clone()
Creates and returns a copy of this object.
clone方法,返回一個object對象的copy
boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
equals方法,屬於最原始的equals,判斷一個對象是否equal to另外一個對象
protected void finalize()
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
當確認這個對象沒有任何的引用時,對一個對象調用大名鼎鼎的GC,垃圾收集器,
Class<?> getClass()
Returns the runtime class of this Object.
返回這個對象的運行時class
int hashCode()
Returns a hash code value for the object.
大名鼎鼎的hashcode方法,返回一個對象的hash code,注意是個整形int
void notify()
Wakes up a single thread that is waiting on this object's monitor.
wakes up一個正在等待當前對象的監視線程,通常翻譯爲喚醒線程
void notifyAll()
Wakes up all threads that are waiting on this object's monitor.
wakes up全部正在等待當前對象的監視線程,
String toString()
Returns a string representation of the object.
大名鼎鼎的toString()方法,返回對象的string 表示
void wait()
Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
大名鼎鼎的wait方法,面試常常會有人問他和sleep方法的區別。。,讓當前線程等待,直到有別的線程調用該對象的notify()方法或者notifyAll()
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.
讓當前線程等待另外一個線程調用notify()方法或notifyAll()方法,或指定的時間運行。那麼什麼是指定時間運行?
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.
讓當前線程等待另外一個線程調用notify()方法或notifyAll()方法,或者其餘的線程中斷當前線程,或指定的時間運行
具體看看方法的說明
public final Class<?> getClass()
Returns the runtime class of this Object
. The returned Class
object is the object that is locked by static synchronized
methods of the represented class.
返回該對象的運行時類。返回的類對象的對象是被靜態同步方法鎖着的類。
The actual result type is Class<? extends |X|>
where |X|
is the erasure of the static type of the expression on which getClass
is called. For example, no cast is required in this code fragment:
Number n = 0;
Class<? extends Number> c = n.getClass();
Returns:
The Class
object that represents the runtime class of this object.
public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap
.
返回一個對象的哈希碼值。這種方法支持哈希表的好處,好比HashMap。
The general contract of hashCode
is:
對於hashcode的通常約束
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode
method must consistently return the same integer, provided no information used in equals
comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
不管什麼時候在不止一次地在對同一對象的Java應用程序的執行調用時,hashCode方法必須始終返回相同的整數,沒有提供信息用於equals比較對象被修改時。對於一個相同應用的不一樣的應用程序的執行來講,這個整數不須要保持一致。
If two objects are equal according to the equals(Object)
method, then calling the hashCode
method on each of the two objects must produce the same integer result.
若是兩個對象根據equals方法判斷是相等的,那麼這兩個對像每個調用hashCode方法都必須產生相同的整數結果。
It is not required that if two objects are unequal according to the equals(java.lang.Object)
method, then calling the hashCode
method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
若是兩個對象根據equals(java . lang . object)方法判斷是不相等的,而後兩個對象調用hashCode方法並非必定會或者說必須會產生不一樣的整數的結果。可是,程序員應該意識到不相等的對象產生不一樣的整數結果可能提升哈希表的性能。
As much as is reasonably practical, the hashCode method defined by class Object
does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)
object類定義的hashCode方法爲不一樣的對象返回不一樣的整數是合理的實用的。(這是典型的實施對象的內部地址轉換成一個整數,可是這個實現技術不須要由Java™編程語言)。
Returns:
a hash code value for this object.
public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
顯示一些其餘對象是否「等於」這一個。
The equals
method implements an equivalence relation on non-null object references:
equals方法對於非空對象的引用實現了一個等價關係
It is reflexive: for any non-null reference value x
, x.equals(x)
should return true
.
自反性,對於任何非空引用x,x.equals(x)必定返回true值
It is symmetric: for any non-null reference values x
and y
, x.equals(y)
should return true
if and only if y.equals(x)
returns true
.
對稱性,對任何的非空引用x、y, x.equals(y)必定返回true值,當且僅當y.equals(x)返回true值
It is transitive: for any non-null reference values x
, y
, and z
, if x.equals(y)
returns true
and y.equals(z)
returns true
, then x.equals(z)
should return true
.
傳遞性,對任何的非空引用x、y、z,若是 x.equals(y)返回true,而且y.equals(z)返回true,那麼
x.equals(z)也應該返回true
It is consistent: for any non-null reference values x
and y
, multiple invocations of x.equals(y)
consistently return true
or consistently return false
, provided no information used in equals
comparisons on the objects is modified.
一致性,對於任何非空引用值x和y,沒有提供用於equals比較對象被修改的信息,屢次調用x.e quals(y)始終返回true或持續返回false
For any non-null reference value x
, x.equals(null)
should return false
.
對於任何非空引用x,x.equals(null)必定要返回false