Java learning notes 18 (Object Class), learning notes object

Source: Internet
Author: User

Java learning notes 18 (Object Class), learning notes object

The Object class is the top-level parent class in Java. All classes are its subclasses and interfaces do not inherit it.

Methods In the Object class:

Official materials: boolean equals (Object obj) indicates whether another Object is "equal" to this Object"

Example:

Package demo; public class Person extends Object {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age;} // rewrite the equals method of the parent class // the source code is the same, equals is to compare the memory address of the two objects public boolean equals (Object obj) {return this = obj;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}}

 

Package demo; public class TestEquals {public static void main (String [] args) {// Person class inherits the Object class, inherited from the parent class method Person p1 = new Person ("Zhang San", 18); Person p2 = new Person ("Li Si", 20); boolean b1 = p1.equals (p2 ); system. out. println (b1); // output false p1 = p2; boolean b2 = p1.equals (p2); System. out. println (b2); // Output true }}

 

However, it is meaningless to compare memory addresses. To meet some requirements, we can rewrite the equals method:

 

Package demo; public class Person extends Object {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age;} // override the equals method of the parent class, and define the comparison method // compare whether the age of the two objects is the same public boolean equals (Object obj) {// process the passed information. if (this = obj) {return true;} // process the null input. if (obj = null) {return false;} // The subclass feature cannot be called in polymorphism, so you need to perform a downward transformation // Add instanceof to prevent risks, because the Object is a top-level parent class, prevent passing in other classes if (obj instanceof Person) {Person p = (Person) obj; return this. age = p. age;} else {return false ;}} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}}
Package demo; public class TestEquals {public static void main (String [] args) {// Person class inherits the Object class, inherited from the parent class method Person p1 = new Person ("Zhang San", 20); Person p2 = new Person ("Li Si", 20); boolean b1 = p1.equals (p2 ); system. out. println (b1); // Output true }}

 

ToString method:

Official materials: String toString () returns the String representation of the object

Continue to write an example of the Person class above:

Package demo; public class TestToString {public static void main (String [] args) {Person p1 = new Person ("Zhang San", 18); String s1 = p1.toString (); System. out. println (s1); System. out. println (p1);}/* here the two outputs are the same: demo.Person@2cdb03a1demo.Person @ 2cdb03a1 so we found that if an object is written in the input statement, the toString method of the object is called by default */

 

In fact, obtaining the memory address has no practical significance. To meet some requirements, we rewrite the toString method:

Package demo; public class Person extends Object {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age;} // override the toString method public String toString () {return this. name + this. age;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}}
Package demo; public class TestToString {public static void main (String [] args) {Person p1 = new Person ("Zhang San", 18); String s1 = p1.toString (); System. out. println (s1); System. out. println (p1) ;}}/* The two outputs are always the same: Zhang San, 18 Zhang San, and 18. We found that if an object is written in the input statement, the toString method of the object is always called by default */

 

There are other methods for Object, some of which are not commonly used, and some will be mentioned later.

 

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.