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 methods are implemented by the Object class in java

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

Share

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

This article will share with you about the methods implemented by the Object class in java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

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

1.clone method

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

2.getClass method

The final method to get the runtime type.

3.toString method

This method is often used, and most subclasses have overrides.

4.finalize method

This method is used to release resources. Because it is impossible to determine when the method will be called, it is rarely used.

5.equals method

This method is a very important one. Generally equals and = = are not the same, but in Object they are the same. Subclasses generally override this method.

6.hashCode method

This method is used for hash lookup. If you override the equals method, you usually override the hashCode method. This method is used in some Collection with hashing function.

Generally, obj1.equals (obj2) = = true must be satisfied. You can deduce obj1.hash-Code () = obj2.hashCode (), but hashCode equality does not necessarily satisfy equals. However, in order to improve efficiency, we should try to make the above two conditions close to equivalence.

7.wait method

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 (longtimeout) sets a timeout interval to return if the lock is not acquired within the specified time.

After calling this method, the current thread goes to sleep until the following event occurs.

(1) another thread called the notify method of the object.

(2) other threads called the notifyAll method of the object.

(3) other threads call interrupt to interrupt the thread.

(4) the time interval is up.

At this point, the thread can be scheduled, and an InterruptedException exception is thrown if it is interrupted.

8.notify method

This method wakes up a thread waiting on the object.

9.notifyAll method

This method wakes up all threads waiting on the object.

-Object-

ClassObjectistherootoftheclasshierarchy.EveryclasshasObjectasasuperclass.Allobjects,includingarrays,implementthemethodsofthisclass.--FromOracle

-interpretation-

The Object class is the parent class inherited by all objects in java, and even the array inherits the parent class (which can be understood as the primitive class, the ancestor of all classes). You might ask: was the class written by James first Object? ).

All classes inherit from the Object class implicitly, so you cannot see it.

-Object-

Default construction method

-clone-

-equals-

Indicateswhethersomeotherobjectis "equalto" thisone.

Theequalsmethodimplementsanequivalencerelationonnon-nullobjectreferences:-FromORacle-

The equals of the original class Object compares references to non-empty objects of two variables.

Source code:

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

From the source code, we can see that the original class equals is actually equivalent to "=".

-finalize-

-getClass-

-hashcode-

IntheJavaprogramminglanguage,everyclassimplicitlyorexplicitlyprovidesahashCode () method,whichdigeststhedatastoredinaninstanceoftheclassintoasinglehashvalue (a32-bitsignedinteger). Thishashisusedbyothercodewhenstoringormanipulatingtheinstance-thevaluesareintendedtobeevenlydistributedforvariedinputsforuseinclustering.Thispropertyisimportanttotheperformanceofhashtablesandotherdatastructuresthatstoreobjectsingroups ("buckets") basedontheircomputedhashvalues.Technically,inJava,hashCode () bydefaultisanativemethod,meaning,ithasthemodifier'native',asitisimplementeddirectlyinthenativecodeintheJVM.

Source:Wikipedia

Each class in java implicitly or explicitly implements the hashcode method of Object.

To sum up with Google and official individuals, why would the author have hashcode in the original class?

①, storage optimization of class objects, easy to find class objects.

②, used with equals.

Note: many blogs say that the hashcode method returns the physical or logical storage address of the class, which is wrong. Officially, the 32-bit value returned is only related to the storage location of the class object.

-notify-

-notifyall-

-toString-

ThetoStringmethodforclassObjectreturnsastringconsistingofthenameoftheclassofwhichtheobjectisaninstance,theat- signsignsigning` @', andtheunsignedhexadecimalrepresentationofthehashcodeoftheobject.Inotherwords,thismethodreturnsastringequaltothevalueof:

GetClass () .getName () +'@'+ Integer.toHexString (hashCode ())

Source code:

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

Returns a hash value of the class in the format + @ +.

-wait-

Finalize ()

Thank you for reading! This is the end of this article on "what methods have been implemented by the Object class in java?" I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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