Java_se Foundation--60. First knowledge of object

Source: Internet
Author: User

Java is the object-oriented language, the core idea: to find the right object to do the right thing:

Method One: Customize the class, and then create the object from the custom class.

Mode Two: Sun provides a lot of classes for me to use, we just need to know these classes, we can create objects through these classes.

The object class is the ultimate parent class for all classes. Any class inherits the object class.

Object: The root class of all classes.
object is constantly extracted and has the common content of all objects.

Class person{      private int age;      Person (int.) {             this.age = age;      }} Class Demo{}class objectdemo{public       static void Main (string[] args) {person            p1 = new Person (a);            person P2 = new person (a);            Person P3 = p1;                  Demo d = new demo ();            System. Out.println (P1 = = p2);//false            system. OUT.PRINTLN (P1.equals (p2));//false            system. OUT.PRINTLN (P1.equals ( p3));//true            System. Out.println (P1.equals (d));//false      }}
Operation Result:

False
False
True
False

The above are compared to the memory address, but not the age, but need to compare the age need to rewrite the Equals method ~

P.S.
The = = and the Equals method of the object class, by default, determine whether two objects are equal based on the hash value of the object.
You can override the comparison rule by overriding the Equals method of object.

Class person{       private int age;       Person (int.) {             this.age = age;       }       Compare the age of person, whether it is peer       //general will cover this method, based on the object's unique content, establish the basis for judging whether the object is the same. Public       Boolean equals (Object obj) {             if (!) ( obj instanceof person)                   throw new ClassCastException ("type error");             Person P = (person) obj;             return this. Age = = P.age;      }} Class objectdemo{public       static void Main (string[] args) {person            p1 = new Person (a);            person P2 = new person (a);            System. Out.println (P1.equals (P2));}      }
Operation Result:

Ture

Class person{      private int age;      Person (int.) {             this.age = age;      }      public int hashcode () {             return age;      }} Class objectdemo{public       static void Main (string[] args) {person            p1 = new Person (a);            System. Out.println (p1);            System. Out.println (P1.getclass (). GetName () + "$" + integer.tohexstring (P1.hashcode ()));}      }
Operation Result:

[Email protected]

Person $21

System. Out.println (P1.getclass (). GetName () + "$" + integer.tohexstring (P1.hashcode ()));
in order to facilitate beginners to understand the above code, the next one to use the method to explain, the following:

1.getClass (). GetName () represents the class name that returns the class to which the object belongs, that is, person.

2.hashCode () represents the hash value of the returned object. (You can also interpret the hash code as the memory address of the object)

3.integer.tohexstring (hashcode ()) represents the object hash value in 16 binary representation.

where Hashcode () is a method defined in the object class that does a hash of the memory address of the objects, returning an int type


ToString (): Returns the string representation of the object, returning a string that describes the object. (returned string representation: hash code for the full class name +@+ object)



AC Penguin: 654249738, and self-taught Exchange group: 517284938

Java_se Foundation--60. First knowledge of object

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.