Understanding of object objects in Java

Source: Internet
Author: User

Java treats everything in reality as an object. Java is object-oriented, that is, object orentied (OO. The object here is defined as a top-level parent class in Java. It is a parent class of any class. We can show that it can be inherited or implicitly inherited, as shown in the following example:

 

Public classDogExtendsObject {

 

}

And

Public classDog {

 

}

 

It is equivalent. So what is the role of an object? Why does each class inherit objects? First, let's take a look at the methods contained in the object.

 

1. Equals (Object OBJ)

2. Finalize ()

3. getclass

4. hashcode ()

5. Policy

6. policyall ();

7. Wait ()

8. Clone ()

9. tostring ()

 

The methods are frequently used. To achieve code reuse, Java designers put these commonly used methods in a class, which is object.

// Tostring

 

Before reading equals, you must first look at "=", because "=" is equal, equals is equal, but "=" is mainly used to judge the equality of basic data types, of course, you can also directly judge two objects. Equals mainly determines the object content. There are many details, which are explained one by one below:

 

First look at "=" to determine the basic data type

 

Public static voidMain (string [] ARGs ){

IntI = 1;

IntJ = 1;

BooleanFlag0 =True;

BooleanFlag1 =False;

System.Out. Println ("I = J:" + (I = j ));

System.Out. Println ("flag0 = flag1:" + (flag0 = flag1 ));

 

}

Running result:

 

I = J: True

Flag0 = flag1: false

 

The above example is very simple, not to mention.

 

Let's take a look at the comparison of objects:

 

Public static voidMain (string [] ARGs ){

Dog dog0 =NewDog ();

Dog dog1 =NewDog ();

System.Out. Println ("dog0 = dog1:") + (dog0 = dog1 ));

}

 

Running result: dog0 = dog1: false

 

In the above example, "=" is used to compare the first address of dog0 memory and the first address of dog1 memory. Because the objects pointed to by dog0 and dog1 are two spaces in the memory, so the first address is different. Print the output here as false.

Is there a way to see the first address of the memory?

There is a saying that "there are always more approaches than problems". If we want to see the first address of the memory, we need to re-read another function tostring (). When we print a reference to an object directly, you can call this method to view an instance:

Public static voidMain (string [] ARGs ){

Dog dog0 =NewDog ();

System.Out. Println ("dog0:") + dog0 );

System.Out. Println ("dog0.tostring ():") + dog0.tostring ());

Print Output:

Dog0: Dog @ 6b97fd

Dog0.tostring (): Dog @ 6b97fd

 

The two are completely equivalent, that is, when we print the output reference directly and call the tostring method on the reference, the two are completely equivalent.

Currently, the dog @ 6b97fd does not have any meaning for us. To make it meaningful, we can rewrite it. For example, when the above instance prints the dog reference, print the dogname of the dog and modify the above instance as follows:

 

Public classDog {

 

PrivateString dogname;

 

PublicString getdogname (){

ReturnDogname;

}

 

Public voidSetdogname (string dogname ){

This. Dogname = dogname;

}

 

 

PublicString tostring (){

Return"Dog's name is:" + dogname;

}

}

 

Entry function:

 

Public classEqualsdemo {

 

Public static voidMain (string [] ARGs ){

Dog dog0 =NewDog ();

Dog0.setdogname ("Xiaobai ");

System.Out. Println ("dog0:") + dog0 );

}

 

 

}

 

Print Output: dog0: Dog's name is: Xiaobai

 

 

In this special case, if two strings with the same content are directly defined, the two will point to the same object:

 

Public static voidMain (string [] ARGs ){

String str0 = "hello ";

String str1 = "hello ";

System.Out. Println ("str0 = str1" + (str0 = str1 ));

}

 

Print Output: str0 = str1: True

 

However, if a String constant is generated using the new method, it does not point to the same object, such as the following instance:

 

Public static voidMain (string [] ARGs ){

String str0 = "hello ";

String str1 =NewString ("hello ");

System.Out. Println ("str0 = str1:" + (str0 = str1 ));

}

 

Run the program and print the output: str0 = str1: false

 

 

The equals method is often used in combination with hashcode. It mainly compares object content, but by default, it compares the first memory address. Check instances:

Create two classes:

 

Public classDog {

 

}

 

Another class:

 

 

 

Public classEqualsdemo {

 

Public static voidMain (string [] ARGs ){

Dog dog0 =NewDog ();

Dog dog1 =NewDog ();

System.Out. Println ("dog0.equals (dog1):") + dog0.equals (dog1 ));

}

 

}

 

Print Output: dog0.equals (dog1): false

 

We can compare the content of two objects by overwriting the equals method. To improve the efficiency, the Java underlying layer first checks whether the hashcode is equal. If the hashcode is equal, it checks whether the equals is equal, if the hashcode is not equal, the system returns false without determining equals. Only when the hashcode is equal and the equals is equal can the two objects be considered completely equal. If the objects are equal, the hashcode must be equal. Why do two objects not necessarily be equal if the hashcode is equal? We can understand that hashcode is the code on each object. For example, assume that hashcode of A is 10, hashcode of B is 11, hashcode of C is 12, and hashcode of D is 13.

The generated object abdhashcode is 10 + 11 + 13 = 34. The hashcode of the object BBC is 11 + 11 + 12 = 34. The hashcode of the two objects is the same, but the content is not the same.

 

The following is an example of combining equals with hashcode. In this example, we want to determine the dog name. If the names are equal, we think that the two objects are equal, if the names are not equal, the two objects are considered unequal.

 

Public classDog {

 

PrivateString dogname;

 

PublicString getdogname (){

ReturnDogname;

}

 

Public voidSetdogname (string dogname ){

This. Dogname = dogname;

}

 

Public intHashcode (){

Final intPrime = 31;

IntResult = 1;

Result = prime * result + (dogname =Null)? 0: dogname. hashcode ());

ReturnResult;

}

 

 

Public BooleanEquals (Object OBJ ){

If(OBJ =Null)

Return false;

 

FinalDog Other = (DOG) OBJ;

If(Dogname. Equals (other. dogname )){

Return true;

}Else{

Return false;

}

}

}

 

The hashcode method obtains and returns the hashcode of the object based on the object attribute and the basic algorithm. The hashcode method is not fixed and can be modified. The equals method is mainly concerned. If (OBJ = NULL) is used to determine whether the input object is null. If it is null, false is returned, and two objects are considered unequal.If(Dogname. Equals (other. dogname) determines whether the content of the dogname string is equal. if the content is equal, true is returned; otherwise, false is returned.

 

The entry functions are as follows:

Public static voidMain (string ARGs []) {

Dog dog0 =NewDog ();

Dog0.setdogname ("Xiaobai ");

DogDog1=NewDog ();

Dog1.setdogname ("Xiaobai ");

System.Out. Println ("dog0.equals (dog1):" + dog0.equals (dog1 ));

}

 

Running result: dog0.equals (dog1): True

 

 

 

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.