Java Object类学习笔记

看下Api文档的一些说明

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 

从JDK1.0就已经存在的元老类,类结构的根,所有类的父类,所有类都实现了这个类的方法,包含arrays。特别强调了arrays。

Constructor Summary

Constructors  Constructor and Description 

Object()  

构造方法介绍。

Modifier and Type Method and Description 

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


你可能感兴趣的:(Java Object类学习笔记)