Think In Object

java.lang.Object is the root of the class hierarchy. Every class has Object as as super class. All objects, including arrays, implement the methods of this class.

Methods:

1. clone(): native method

creates and returns a copy of this object. The precise meaning of ‘copy’ may depend on the class of this object. FOr any object x.

1
2
3
x.clone()!=x
x.clone().getClass()==x.getClass()
x.clone().equals(x) ???

This method performs a ‘shallow copy’ of this object, not a ‘deep copy’ operation. If the class of this object does at not implement the interface cloneable, then a CloneNotSupportedException is thrown.


2. getClass(): native method

returns the runtime class of this object.


3. equals(Object obj):
1
2
3
{
return (this==obj);
}

This method implements the most discriminating possible equivalence relation on objects. That is , for any non-null reference value x and y, this method returns true if and only if x and y refer to the same object(x==y:true).
Note that it is generally necessary to override the hascode() method whenever thie method is overriddem, so as to maintain the general contract for hascode() method, which states that equal objects must have equal hash codes.


4. toString():
1
2
3
{
return getClass.getName()+"@"+Integer.toHexString(hashcode()).
}

5. hashcode():

Return a hash code value for the object. The general contract of hash code is:

  • 在Java应用的一次执行过程中,如果对象用于equals()比较的信息没有被修改,那么同一个对象多次调用hashcode()方法应该返回同一个整型值. 应用多次执行中,这个值不需要保持一致,即每次执行都是保持着各自不同的值.
  • 如果两个对象的equals()方法判断相等,那么两个对象的hashcode()必须得到相同的值.
  • 如果两个对象的equals()方法判断不相等,那么它们的hashcode()可以返回相等的值,也可以返回不相等的值.

6. notify() & notifyAll():

Wakes up a single thread(all threads) that is waiting on this objects monitor.


7. wait() & wait(long int) & wait(long timeout):

Cause the current thread to wait until either another thread invokes the notify()&notifyAll(), or a special amount of time has elapsed.


8. finalize():