The object class of java--face

Source: Internet
Author: User

First, the concept

The Object class is the root class in the Java language, which is the parent class of all classes. All of the method subclasses described in it can be used. All classes when creating objects, the final parent class to find is object. in Many methods of the Object class, we first learn the equals method and the toString method, Other methods will be learned later in the course.

second,equals method

The Equals method, which is used to compare two objects , is actually compared with the memory address of two objects. the equals method inside the Object class is used internally by the = = comparison operator. in development, to compare two objects is the same, often based on the value of the property in the object comparison, that is, in development often requires subclasses to override the Equals method to compare the property values of the object. The following code shows:

1 /*2 describe people in this class, and define the function according to age to determine whether it is peer3 because you want to compare the properties of a specified class, just overwrite the Equals method in Object4 To compare the property values of a class in a method body5  */6 classPersonextendsobject{7     intAge ;8     //The equals method of the replication parent class to achieve its own comparison9      Public Booleanequals (Object obj) {Ten         //determines whether the object that is currently calling the Equals method is the same as the object passed in One         if( This==obj) { A             return true; -         } -         //determines whether the passed in object is a person type the         if(! (objinstanceofPerson )) { -             return false; -         } -         //Transform obj down to a perosn reference, accessing its properties +Person p =(person) obj; -         return  This. Age = =P.age; +     } A}

Note : In the replication The equals method in object, be sure to note that the parameters of the public boolean equals (Object obj) are type object, which must be type- cast when invoking the properties of an object, and type-judging before conversion.

third,toString method

The ToString method returns the string representation of the object , in fact the string content is the type of the object [email protected]+ memory address value.

because the toString method returns the memory address, in development, it is often necessary to get the corresponding string representation of the object's properties, so it needs to be rewritten as well.

1 class extends object{2     int Age ; 3     // override the ToString method based on the property of the person class 4      Public String toString () {5         return "Person [age=" + Age + "]"; 6     }7 }

For example:

Person class

1  Public classPerson {2     PrivateString name;3     Private intAge ;4 Person () {5         6     }7Person (String name,intAge ) {8          This. name=name;9          This. age=Age ;Ten     } One  A      Public BooleanEquals (Object obj) {//Object obj=new person (); -         //calling a member of a subclass that needs to be transformed downward -         if(obj==NULL){ the             return false; -         } -         if(obj== This){ -             return true; +         } -         if(objinstanceofPerson ) { +Person p=(person) obj; A             return  This. age==P.age; at         } -         return false; -     } - @Override -      PublicString toString () { -         return"Person [name=" + name + ", age=" + Age + "]"; in     } -      to     /*Public String toString () { +          - return "Name:" +name+ ", Age:" +age; the     }*/ *      $}

Test class

1  Public classDemo01 {2 3      Public Static voidMain (string[] args) {4Person p1=NewPerson ("Big Bear", 2);5Person p2=NewPerson ("Bear II", 2);6Arraylist<string> list=NewArraylist<string>();7System.out.println (p1==p2); False8 System.out.println (P1.equals (list)); False9System.out.println (p1);//The default print ToString () subclass method is overridden to become a subclass override methodTen System.out.println (p2); One}

The object class of java--face

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.