What are the common methods of object?

Source: Internet
Author: User

object is the parent class of all classes, and any class inherits object by default. What exactly does the object class implement?

1. Clone method

Protection method, implement shallow copy of object, only implement the Cloneable interface can call this method, otherwise throw clonenotsupportedexception exception.

Basically Java in addition to 8 basic types of parameters is the value of the transfer, the other class object parameters are reference passing, we sometimes do not want to say in the method parameter changes, this is the need to replicate the Clone method in the class.

2. GetClass method

Final method, which gets the run-time type.

3. ToString Method

This method is used more and the general subclass has coverage.

4. Finalize method

This method is used to release resources. It is seldom used because it is not possible to determine when the method is invoked.

5. Equals method

This method is a very important method. The general equals and = = are not the same, but the two are the same in object. Subclasses generally have to override this method.

6. Hashcode method

This method is used for hash lookups, and overriding the Equals method generally overrides the Hashcode method. This method is used in some collection with hash functions.

The Obj1.equals (OBJ2) ==true must be met in general. Obj1.hash-code () ==obj2.hashcode () can be introduced, but hashcode equality does not necessarily satisfy equals. However, in order to improve efficiency, we should try to make the above two conditions nearly equivalent.

7. Wait method

The wait method is to have the current thread wait for the lock on the object, which must be the owner of the object, which is the lock that owns the object. The wait () method waits until the lock is acquired or interrupted. Wait (long timeout) sets a time-out interval that returns if the lock is not acquired within the specified time.

When the method is called, the thread goes to sleep until the following event occurs.

(1) The Notify method of the object is called by other threads.

(2) The Notifyall method of the object is called by other threads.

(3) Another thread called Interrupt interrupts the thread.

(4) The time interval is up.

At this point the thread can be dispatched, and if it is interrupted, throws a Interruptedexception exception.

8. Notify method

The method wakes up a thread waiting on the object.

9. Notifyall method

The method wakes up all the threads waiting on the object.

What are the common methods of object?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.