Object Class,

Source: Internet
Author: User

Object Class,
1 Overview of Object classes

  • Class Object is the root class of the class hierarchy. Each class usesObjectAs a superclass. All objects (including arrays) implement this class method.
  • Object classes are constantly extracted.

 

2 equals () method
public boolean equals(Object obj)
public boolean equals(Object obj) {        return (this == obj);    }

 

  • Example: Call the equals () method of the Object class
Package java011;/**** * Description: */public class Person {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age ;}}
Package java011;/***** * Description: */public class Test {public static void main (String [] args) {Person p = new Person ("James ", 15); Person p2 = new Person ("Zhang San", 15); System. out. println (p = p2); // false System. out. println (p. equals (p2); // false }}

 

  • Example: override the equals () method
Package java011;/**** * Description: */public class Person {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age ;}@ Override public boolean equals (Object obj) {if (obj = null) {return false;} if (obj instanceof Person) {Person p = (Person) obj; if (! This. name. equals (p. name) & this. age! = P. age) {return false ;}} return true ;}}
Package java011;/***** * Description: */public class Test {public static void main (String [] args) {Person p = new Person ("James ", 15); Person p2 = new Person ("Zhang San", 15); System. out. println (p = p2); // false System. out. println (p. equals (p2); // true }}

 

3 hashcode () method
public int hashCode()
public native int hashCode();

 

Package java011;/**** * Description: */public class Person {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age ;}@ Override public boolean equals (Object obj) {if (obj = null) {return false;} if (obj instanceof Person) {Person p = (Person) obj; if (! This. name. equals (p. name) & this. age! = P. age) {return false ;}} return true ;}}
Package java011;/***** * Description: */public class Test {public static void main (String [] args) {Person p = new Person ("James ", 15); Person p2 = new Person ("Zhang San", 15); System. out. println (p); // java011.Person @ 1b6d3586 System. out. println (p. hashCode (); // 460141958 System. out. println (Integer. toHexString (p. hashCode (); // 1b6d3586 }}

 

4 getClass () method
public final Class<?> getClass()
public final native Class<?> getClass();

 

  • Example:
Package java011;/**** * Description: */public class Person {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age ;}@ Override public boolean equals (Object obj) {if (obj = null) {return false;} if (obj instanceof Person) {Person p = (Person) obj; if (! This. name. equals (p. name) & this. age! = P. age) {return false ;}} return true ;}}
Package java011;/***** * Description: */public class Test {public static void main (String [] args) {Person p = new Person ("James ", 15); Class <?> Clazz = p. getClass (); System. out. println (clazz); // class java011.Person System. out. println (clazz. getName (); // java011.Person }}

 

5 toString () method
public String toString()
public String toString() {        return getClass().getName() + "@" + Integer.toHexString(hashCode());    }

 

  • Example:
Package java011;/**** * Description: */public class Person {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age ;}@ Override public boolean equals (Object obj) {if (obj = null) {return false;} if (obj instanceof Person) {Person p = (Person) obj; if (! This. name. equals (p. name) & this. age! = P. age) {return false ;}} return true ;}}
Package java011;/***** * Description: */public class Test {public static void main (String [] args) {Person p = new Person ("James ", 15); System. out. println (p); // java011.Person @ 1b6d3586 System. out. println (p. toString (); // java011.Person @ 1b6d3586 }}

 

Related Article

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.