12 commonly used method in Java Object class, you used a few?

Foreword

Object methods in Java is a very high frequency of the point in the interview, after all, it is the Object of all classes "ancestors." All Java classes have a common ancestor Object class, subclass will inherit all the public methods of the Object class.

v2-85a5500cdcad01b8c8283c93e2610bed_hd.png

Object classes look at the structure of (shortcut: alt + 7):

v2-1bb86a82a8f707138d47332b8fd1b152_hd.jpg

1. getClass method

public final native Class<?> getClass();

final method, when the operation for acquiring an object class object, the object class is the object that belongs to the class described. This method is usually Java reflection and with use.

2. hashCode method

public native int hashCode();

This method is mainly used for the hash value acquisition object. Object of this method returns the default heap memory address of the object.

3. equals method

public boolean equals(Object obj) {        return (this == obj);}

The method used to compare two objects, if the two object references point to the same object, it returns true, otherwise false. Generally equals and == is not the same, but in Object two are the same. Subclass generally required to override this method.

v2-4c103b2a36057f2a54b74a35157eef52_hd.png

4. clone Method

protected native Object clone() throws CloneNotSupportedException;

This method is protected by method to achieve the object of a shallow copy, only to achieve the Cloneable interface can call this method, otherwise throw a CloneNotSupportedException.

The default is shallow copy clone. The so-called shallow copy, refers to the object within the object attribute references cited only copy the address, the object will not re-allocate memory references. Deep copy the object reference will not even be re-created.

5. toString Method

public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

返回一个 String 对象,一般子类都有覆盖。默认返回格式如下:对象的 class 名称 + @ + hashCode 的十六进制字符串。

6. notify 方法

public final native void notify();

final 方法,主要用于唤醒在该对象上等待的某个线程。

7. notifyAll 方法

public final native void notifyAll();

final 方法,主要用于唤醒在该对象上等待的所有线程。

8. wait(long timeout) 方法

public final native void wait(long timeout) throws InterruptedException;

wait 方法就是使当前线程等待该对象的锁,当前线程必须是该对象的拥有者,也就是具有该对象的锁。wait() 方法一直等待,直到获得锁或者被中断。wait(long timeout) 设定一个超时间隔,如果在规定时间内没有获得锁就返回。欢迎大家关注我的公种浩【程序员追风】,文章都会在里面更新,整理的资料也会放在里面。

v2-0af38a01e39be5ee941bf532cee66505_hd.png

9. wait(long timeout, int nanos) 方法

public final void wait(long timeout, int nanos) throws InterruptedException {
  if (timeout < 0) {
    throw new IllegalArgumentException("timeout value is negative");
  }

  if (nanos < 0 || nanos > 999999) {
    throw new IllegalArgumentException(
              "nanosecond timeout value out of range");
  }

  if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
    timeout++;
  }

  wait(timeout);
}

Parameter Description

timeout: maximum waiting time (in milliseconds)

nanos: additional time in the millisecond range (0-999999)

This method causes the current thread to wait until another thread invokes the object's notify () method or the notifyAll () method, or at a specified time has elapsed. This method is similar to a wait parameter method, but it allows better control of the waiting time before a notification amount to give up. Real amount in nano seconds, is calculated as follows:

1000000 * timeout + nanos

In all other respects, this method wait (long timeout) do the same thing. In particular, wait (0, 0) represents and wait (0) the same.

10. wait Method

public final void wait() throws InterruptedException {  wait(0);}

We can see wait () method actually calls wait (long timeout) method, except timeout to 0, that does not wait.

11. finalize method

protected void finalize() throws Throwable { }

This method is the protection method is mainly used to be called again in the GC, if we implement this method, the object may be revived again in this process, so as to avoid being recovered GC.


At last

Welcome to share with everyone, like the point of a praise yo remember the article, thanks for the support!


Guess you like

Origin blog.51cto.com/14442094/2446343