In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains how to understand the Object source code on the basis of 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 how to understand the Object source code on the basis of Java.
GetClass
Public final native Class getClass ()
GetClass is also a native method. The function of this method is to return the runtime class of an object. Its return value is of type Class, Class c = obj.getClass (); through object c, we can get all the member methods of the object, each of which is a Method object; we can also get all the member variables of the object, each of which is a Field object Similarly, we can get the constructor of the object, which is a Constructor object. This method is often used in reflection.
HashCode
Public native int hashCode ()
The hashCode method returns a hash value.
The return value is translated by default from the address of the object.
The return value of a call to hashCode from the same object is equal.
If the equals of two objects is equal, then the hashCode must be equal.
If the equals of two objects is not equal, then the hashCode is not necessarily equal.
Equals
Public boolean equals (Object obj) {return (this = = obj);}
The implementation of equals is very simple, its function is to compare whether two objects are equal, and the comparison is based on the memory address of the two. In addition, equals follows the following principles:
1. Reflexivity: x.equals (x); / / true 2, symmetry: x.equals (y) = = y.equals (x); / / true 3, transitivity: if (x.equals (y) & & y.equals (z)) x.equals (z); / / true; 4, consistency. As long as the object has not been modified, the result of calling the equals () method many times remains unchanged: x.equals (y) = x.equals (y) / / true 5, non-null. Call x.equals (null) on any object that is not null. The result is false: x.equals (null); / / false
Why rewrite hashcode and equals?
Because both methods are related to the comparison of objects, if you want to compare objects in the program, there is a good chance that you will override these two methods. Because the default comparison logic of equals is to compare the addresses of objects, the memory addresses of the two objects must be different, so in any case, the two objects must return false through eqals comparison.
But in actual programming, we often encounter deduplication, or put objects in an ordered collection, or store objects in a collection without repetition, at this time, if we do not rewrite equals and hashCode, we will not be able to meet the requirements.
For example, there are two objects in the system, object An and object B, whose names and ID numbers are exactly the same. At this point, there are two objects in the system memory, but their contents are consistent. It is clear that one person produces two duplicate messages at the same time. If you compare using the default equals method, the two objects are never equal and can never be compared to the same duplicate message. Therefore, the equals and hashCode methods should be rewritten to achieve the above requirements.
Clone
Protected native Object clone () throws CloneNotSupportedException
Clone () is the protected method of Object, but it is not public. If a class does not explicitly override clone (), other classes cannot directly call the clone () method of this class instance. In addition, several important points are mentioned in Clone's comments:
The cloned object must implement the Cloneable interface and override the clone method, otherwise a CloneNotSupportedException exception will be reported
The clone () method is not a method of the Cloneable interface, but a protected method of Object. The Cloneable interface simply states that if a class does not implement the Cloneable interface and calls the clone () method, a CloneNotSupportedException will be thrown.
Shallow copy: the reference type of the copy object and the original object refers to the same object.
Deep copy: the reference type of the copy object and the original object refers to different objects.
We will discuss shallow copy and deep copy later.
ToString
Public String toString () {return getClass () .getName () + "@" + Integer.toHexString (hashCode ());}
Returns a string representation of the object, a very important method
GetClass (). GetName (); get the corresponding full path name of the bytecode file, such as java.lang.Object
Integer.toHexString (hashCode ()); converts the hash value to a string in hexadecimal format.
Wait and notify
Public final void wait () throws InterruptedException {wait (0);} public final native void wait (long timeout) throws InterruptedException; 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 > 0) {timeout++;} wait (timeout);}
The role of wait is to put the current thread into a waiting state, while wait () also causes the current thread to release the locks it holds. Until another thread calls the object's notify () method or notifyAll () method, the current thread is awakened to the ready state.
Wait (long timeout) (in milliseconds) keeps the current thread waiting (blocking) until another thread calls the object's notify () method or notifyAll () method, or after a specified amount of time, the current thread is awakened to the ready state.
Wait (long timeout, int nanos) is the same as wait (long timeout), the only difference is that this provides higher precision. The total timeout (in nanoseconds) is calculated as 1000000 * timeout+ nanos. By the way, wait (0) and wait (0) have the same effect.
Ublic final native void notify (); public final native void notifyAll ()
The first is notify. The role of notify is to randomly wake up a thread in the waiting queue, while notifyAll is to wake up all threads in the waiting queue.
Note: specifications for the use of notify and wait methods. This means that both must be used in synchronized-decorated synchronization methods or synchronization code.
What's the difference between Thread.sleep () and Object.wait ()?
First, both can pause the current thread and release control of the CPU. The main difference is that Object.wait () releases control of the object lock while releasing the CPU. Thread.sleep () does not release the lock. In other words, sleep is to play hooligans and occupy the manger without taking a shit.
Complete code
Package java.lang; public class Object {/ * A local method, specifically implemented in DLL with C (C++), and then called * / private static native void registerNatives () through JNI; / * this method is automatically called when the object is initialized * / static {registerNatives () } / * returns the runtime class of this Object * / public final native Class getClass (); the general protocol of / * hashCode is: * 1. During the execution of a java application, when the hashCode () method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used to compare the object for equals has not been modified. * from one execution of an application to another execution of the same application, the integer does not need to be consistent. * 2. If two objects are equal according to the equals (object) method, calling the hashCode method on each of the two objects must produce the same integer result. * 3. If two objects are not equal according to the equals (java.lang.Object) method, calling the hashCode () method on either of the two objects does not necessarily produce different integer results. * however, programmers should be aware that generating different integer results for unequal objects can improve the performance of hash tables. * / public native int hashCode (); / * the memory address of the object is compared here * / public boolean equals (Object obj) {return (this = = obj);} / * Local clone method for object replication * / protected native Object clone () throws CloneNotSupportedException / * returns a string representation of the object. The very important method is * getClass (). GetName (); get the corresponding full-path name of the bytecode file, such as java.lang.Object * Integer.toHexString (hashCode ()); and convert the hash value to a string in hexadecimal format. * / public String toString () {return getClass (). GetName () + "@" + Integer.toHexString (hashCode ());} / * * cannot be overridden to wake up a thread that is in a waiting state (waiting or time_wait) due to waiting for the object (the wait method is called), which can only call * / public final native void notify () in a synchronous method or synchronization block. / * cannot be overridden to wake up all threads in a waiting state (waiting or time_waiting) due to waiting for the object (calling the wait method), which can only call * / public final native void notifyAll () in synchronous methods or synchronous blocks. / * cannot be overridden. It is used to cause the current thread to enter a waiting state (time_waiting) in milliseconds in a thread call. This method can only be called in a synchronous method or synchronization block, and the thread can re-enter the runnable state after the setting time is exceeded * / public final native void wait (long timeout) throws InterruptedException; 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 > 0) {timeout++;} wait (timeout);} / * * causes the current thread to wait before another thread calls the object's notify () method or notifyAll () method. In other words, this method behaves as if it only made a wait (0) call. * the current thread must have this object monitor. * the thread publishes ownership of the monitor and waits until another thread wakes up by calling the notify method or the notifyAll method to notify the waiting thread on this object's monitor, * then the thread will wait until it has regained ownership of the monitor before continuing to execute. * / public final void wait () throws InterruptedException {wait (0);} / * this method is called when the object is reclaimed, which is supported by JVM. The finalize method of Object does nothing by default. If the subclass needs to perform some logical processing when the object is reclaimed, you can override the finalize method. * / protected void finalize () throws Throwable {}} at this point, I believe you have a deeper understanding of "how to understand the Object source code on the basis of 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.