Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common methods of Object class in Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the common methods of Object class in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the common methods of Object class in Java.

Preface

The Object method in Java is a very frequent point in the interview. After all, Object is the "ancestor" of all classes. All classes in Java have a common ancestor, the Object class, and subclasses inherit the public methods in all Object classes.

Let's take a look at the class structure of Object:

1. GetClass method

Public final native Class getClass ()

The final method, the runtime class object that gets the object, and the class object is the object that describes the class to which the object belongs. This method is usually used in conjunction with the Java reflection mechanism.

2. HashCode method

Public native int hashCode ()

This method is mainly used to obtain the hash value of the object. This method returns the heap memory address of the object by default in Object.

3. Equals method

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

This method is used to compare two objects, returning true if the two object references point to the same object, or false otherwise. Generally equals and = = are not the same, but in Object they are the same. Subclasses generally override this method.

4. Clone method

Protected native Object clone () throws CloneNotSupportedException

This method protects the method and implements the shallow copy of the object. Only when the Cloneable interface is implemented can the method be called, otherwise a CloneNotSupportedException exception is thrown.

The default clone method is shallow copy. The so-called shallow copy means that objects referenced by attributes within an object will only copy the reference address and will not reallocate memory for the referenced object. A deep copy recreates even the referenced objects.

5. ToString method

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

Returns a String object, which is generally overridden by subclasses. The default return format is as follows: the class name of the object + @ + hexadecimal string of hashCode.

6. Notify method

Public final native void notify ()

The final method is primarily used to wake up a thread waiting on the object.

7. NotifyAll method

Public final native void notifyAll ()

The final method is mainly used to wake up all threads waiting on the object.

8. Wait (long timeout) method

Public final native void wait (long timeout) throws InterruptedException

The wait method is to make the current thread wait for the object's lock, and the current thread must be the owner of the object, that is, the lock with the object. The wait () method waits until the lock is acquired or interrupted. Wait (long timeout) sets a timeout interval to return if the lock is not acquired within the specified time.

9. Wait (long timeout, int nanos) method

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 wait time (milliseconds)

Nanos: additional time in millisecond range (0-999999)

This method causes the current thread to wait until another thread calls the object's notify () method or notifyAll () method, or at a specified elapsed time. This method is similar to a parameter of the wait method, but it allows better control over the amount of time before waiting for a notification to be abandoned. Real-time quantity, calculated in nanoseconds, the formula is as follows: 1000000 * timeout + nanos

In all other respects, this approach does the same thing as wait (long timeout). In particular, wait (0,0) means the same as wait (0).

10. Wait method

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

You can see that the wait () method actually calls the wait (long timeout) method, except that timeout is 0, which means you don't wait.

11. Finalize method

Protected void finalize () throws Throwable {}

This method is a protection method and is mainly used to be called again during GC. If we implement this method, the object may be resurrected in this method to avoid being reclaimed by GC.

At this point, I believe you have a deeper understanding of "what are the common methods of the Object class in Java?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report