In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about what Class and Object mean 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.
At the beginning of learning, the first thing the teacher said to us was that everything was an object in the world of Java. So what is this object? It is the Object object under the java.lang package. Object is a concrete class that is designed to extend. The description of Object in JDK is as follows:
It means that Object is root at the class level and is the parent class of each class. All objects, including arrays and methods in classes.
1. The relationship between Object and interface
But there is no mention of the interface. Moreover, the hierarchical structure of querying objects and interfaces in IDE is also different:
We can see the class representation in java.util.Timer.
Timer.class
But we can use Ctrl + h to see that its class structure is as follows:
Then let's take a look at the class inheritance architecture of interface java.util.concurrent.Future in JDK.
But we can take a look at the Java virtual machine specification-9.2. Interface Members .
If there is no direct parent interface, the interface declares a public abstract method mJVM will implicitly define a method with exactly the same method signature in the Object class for the interface tag. Unless the same method signature is clearly defined in the interface, the same return value and the same exception as throws are returned.
But pay attention to the following two points:
If the same method is defined in the interface as defined by Object and declared as final. Then a compilation exception will occur.
If the same method is defined in the interface as the Object definition signature. But it doesn't conform to Object's rewriting specification. It means that the return value type is different, or an incompatible exception is thrown, or it is not an abstract method. Compilation exceptions can also occur.
2. Methods in Object
Let's take a look at the methods in Object. Object as a general class, as a general class can not satisfy a variety of business scenarios, so there are clear general conventions for all its non-final methods (equals, hashcode, toString, clone and finalize), because they are designed to be overridden. Any class that overrides these methods is responsible for following these general conventions, and other classes that rely on these conventions (such as HashMap and HashSet) cannot work with this class.
2.1 getClass-gets the class type of the current object
Gets the class type of the current object, which you can use to perform reflection operations.
2.2 hashCode-gets the hashCode of the current object
Returns the hash code value of the current object. This method needs to be overridden when the object needs to use the hash table. You also need to override the equals method.
2.3 equals-determines whether two objects are equal
Indicates whether other objects are equal to the current object. The default is to determine whether the reference addresses of two objects are equal. When it is recommended to override the equals method in Effective Java, always override the hashCode method. When you need to override the equals method, you need to follow its general rules. Here's what we agreed on:
Reflexivity (reflexive): for any reference value that is not null, true. Equals (x) must be returned.
Symmetry (symmetric): for any reference value that is not null, x.equals (y) must return true if and only if y.equals (x) returns true.
Transitivity (transitive): the reference value for any non-null, x _ journal y ~ z. If x.equals (y) returns true. And y.equals (z) also returns true, so x.equals (z) must also return true.
Consistent: for any non-null reference values x and y, multiple calls to x.equals (y) will consistently return true or false as long as the information used by the equals comparison operation in the object has not been modified.
False must be returned for any reference value xmae x.equals (null) that is not null
2.4 clone-object cloning
Create and return the copy of this object. The exact expression of "copy" depends on the class of the object. In general, for object x:
Expression: x.clone ()! = x, will be true
Expression: x.clone () .getClass () = = x.getClass (), will be true
Expression: x.clone (). Equals (x), which will be true.
This situation is satisfied by default, but this is not absolute.
According to the convention: generally, the returned object is obtained by calling super.clone. If a class and all its superclasses appear: x.clone (). GetClass () = = x.getClass ().
And the Object class does not implement the Cloneable interface. So if a class does not implement the Cloneable interface, and its instance also calls the clone method, CloneNotSupportedException.
2.5 toString-returns the String that represents this object
Although java.lang.Object provides an implementation of the toString method, it returns the name of the class and an "@" symbol, followed by an unsigned hexadecimal representation of the hash code. For example: Object@163b91, this representation is very unfriendly to the user. So the convention of toString is: it is recommended that all subclasses override this method.
2.6 notify-Wake up a waiting thread monitored by the current thread
Wakes up a thread that is being monitored by the current thread. If there are many threads waiting in this object, one of them will be selected to wake up. This choice is made arbitrarily according to different systems, that is, multiple threads randomly select a thread to wake up in waiting.
This method can only be called by the monitor of this object. A thread can become the owner of the monitor of this object in three ways.
Executes the synchronous instance method of this object.
Executes the synchronization code block for this object.
For the Class object, execute a synchronous static method.
And only one thread can be the owner of an object minitor at a time.
2.7 notifyall-Wake up is being monitored by the current thread waiting for all threads
Wake up is being monitored by the current thread waiting for all threads. If a thread is monitor by this object, you can call the wait method.
This method can only be called by the owner of the monitor of this object. Take a look at the notity method that describes how a thread can succeed as an owner of a monitor.
2.8wait () | | wait (long) | | wait (long, int)
The function of all three methods is the same, which is to put the thread into a waiting state. It's just that the timeout for waiting is set differently. All three methods are called to pause the current thread until another thread calls the notify method or the notifyall method or another thread interrupts the current thread or expires the timeout.
2.9 finalize-call garbage collection for this object
When there is no reference to the object, the object's garbage collection is called. Subclasses can override finalize methods to handle system resources or perform other cleanup operations.
In general, the finalize method is called when JVM determines that the object is not referenced by another object. General Class written in pure Java does not need to override this method, because Object already implements a default, unless we want to implement special functionality (which involves a lot of things, such as object space trees, etc.).
However, for Class written in code other than Java (such as the memory allocated by JNI,C++ 's new method), the garbage collector will not collect these parts correctly, so we need to override the default method to achieve the correct release and collection of this part of memory (for example, C++ needs delete).
In short, finalize is the equivalent of a destructor, and it is the first method to be called when an object is reclaimed by the garbage collector. However, because Java's garbage collection mechanism automatically does these things for us, we don't need to release them manually under normal circumstances.
3. Object and Class
Class or Object first?
In the object model of Java:
All classes are instances of the Class class, and Object is the class, so Object is also an instance of the Class class.
All classes eventually inherit from the Object class, and Class is the class, so Class inherits from Object.
Thank you for reading! This is the end of the article on "what is the meaning of Class and Object 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 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.
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
Set attribute and filter the desired fields
© 2024 shulou.com SLNews company. All rights reserved.