Java-Preliminary Understanding-11th Chapter-object class-equals method

Source: Internet
Author: User
Tags object object

One.

Next introduce a special object, learn the inheritance, polymorphism, will be talked about, before also touched.

Previously discussed in the inheritance system sub-parent class, there has always been a super () call to the parent class, and the parent class also has a super () call to object. Now is the discussion object object.

object is the root class of all the objects in Java, either directly or indirectly, and indirectly in succession, and the parent class above is constantly copied. Even if a program does not inherit, the system still has an inheritance, which is related to the object structure.

How did this object come about? Java is composed of objects, the common object of the general extraction, and constantly extracted, and eventually formed the object class.

This means that the methods in the object class are available for all objects.

Two.

Now let's take a brief look at the object class, including the contents of the class.

There is an object constructor in the object class, which does not have a constructor method for the parent class, which is the final class. → All classes have constructors.

There are a number of well-defined methods in the object class (which I think are intrinsic, system-wide), which are introduced in part to facilitate later application.

First, the method described is the Equals (object obj) method. This method in the video is to compare whether two objects are "equal". We can't control how to judge equality, but what is the standard of equality? Here it is said that the type of the result is a Boolean type, which shows what function is a means of operation, and finally, to obtain the result. We use the function to process the data and finally get the result. → This is my understanding of the function.

The argument list of the Equals (object obj) method is a single formal parameter. Why the parameter is one, because this object is called, and another object of the input is compared, there is an object that is OK. There is also a very important point of reference for the input of formal parameters , just comparing objects is not equal, as to what type of object is not limited, so use object to represent its type, this is the embodiment of polymorphism, the object itself is certainly not an object type.

Now take an example to illustrate the use of the Equals () method.

(Here is a little doubt, = = and equals () method What is the difference? One is an operator, the other is a function function, and the two are not the same, but they all seem to be operations. )

Whether the two are the same, the most straightforward is to verify by compiling the run.

Both are errors, the above account of the object equality is what meaning, is an object. That is, multiple references point to the same object. Is the comparison object equal to the = = or Equals () method ? two ways to say the equivalent exactly what is not yet understood in depth . My personal understanding is that = = refers to the address number of the comparison object. Equals () represents the comparison of the contents of two objects.

At this time, modify the program, run again, and look at both methods.

According to the video explanation, the two methods compare the object's address code. so why set two functions the same thing? Is it because one is shorthand and one is the form of a function ?

Why compare objects, compare encoded addresses? I think the object of the content is very diverse, it is difficult to compare one by one, rather than directly compare the entire object's address is easier, or the same, or different. → Personal understanding.

Why is the Equals () method compared to the object address, what is the basis?

Take a look at the source code of the object class

Static shared, private, native is local, native is a description of the local method, there is this modification means that this method of the subject content, you do not need to know, nor will you know. The main content of this method will eventually be converted to the code that calls the underlying C, which invokes the system content, so this is the local method, which is stored in the local method area. You can't call this method, you can't call it.

The second sentence in the above is a static block of code, and as the class is loaded, it executes "register locally", registernative, to register the root class on the local system. This is the class that is loaded and must be executed, regardless of whether the class created the object. This is the static code block application, which is called the direct completion of the registration action, as for the production of objects without the Tube, class loading must be done first, do not do it late.

Look down, there are many local methods. (Existence of native)

How the local method is implemented, we do not know, this depends on the system, the system is different, it will invoke the existing system to implement itself.

Here is the Equals method, which compares the current object with the specified object. The method body uses = =, so it is the same as = =. That's what we're talking about. The Equals method compares the address encoding. In order to be able to receive more objects, it uses object as a parameter and expands the type. With a limit of = =,

Modify the program again to deepen your understanding of the code,

The D of the demo class is passed in, because the Equals method parameter is set to the object class, so that D is also possible.

Java-Preliminary Understanding-11th Chapter-object class-equals method

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.