Java Object Class

Source: Internet
Author: User

Package cn.fish.object;/*java is the object-oriented language, the core idea: Find the right object for   do the right thing:     way one: custom classes, The object is then created from a custom class.      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 class: Common methods of     object class:         tostring ();  returns the string representation of the object.   Returns a string that describes the object.              question:  tostring ()   What is the role?    after we rewrite ToString, we output an object directly, and we will print out the format data that we need.         equals (object obj)     for comparing memory addresses of two objects, Determines whether two objects are the same object.         hashcode ()    Returns the hash code value of the object (the hash code can be interpreted as the memory address of the object)   Specifications in            java: Generally we override the Equals method of a class, We all rewrite its Hashcode method.         java is open source .... Source Code Disclosure ... How to view source code:     mode one:  holdCTRL key, click the source code you need to see .     way two: Move the cursor to the point where you need to view the code to press F3. Why do we want to view the source code:     1, look at the source code can understand how others write this technology, let us understand more in-depth.     2.  absorbing Daniel's ideas.      See source code The most taboo point:  not every line of code to understand what the meaning, can see a guess what he means is enough.  */ class Person{    int id;    String name;         public person (Int id, string name)  {         this.id = id;         this.name = name;    }         public person ()  {    }        // At the moment I need to output an object directly, the format of the output is:  number:110  name:  Dog Doll this format.   The     //tostring method of object currently does not meet the needs of subclasses, then we should rewrite the ToString of the object class.      @Override     public&nbsp String tostring ()  {        return  "No.:" + this.id  +  "  Name:" +this.name;    }       // Why rewrite: The Equals method of object compares the memory address of two objects by default, and I need to compare the ID of two objects, so the Equals method of the object class does not meet my current requirement.      @Override     public boolean equals (object obj)  {         Person p  =  (person) obj;         return this.id== p.id;    }          @Override     public int hashcode ()  {        return  this.id;    }} Public class demo1 {    public static void main (String[]  args)  {         /*        object o = new object ();         system.out.println (O.tostring ());  // [email  The string returned by protected]   represents the hash code for the:  full class name [email protected]+  object          system.out.println (o);  //  by looking at the source code, the direct output of an object, The ToString method of this call is actually called inside the println method, outputting the content returned by the ToString method.                 //Questions:  Why is it the same as the result of the string returned by the ToString method of the output object when I output an object directly?         person  p1 = new person (110, "Dog Doll");         system.out.println ("P1:" +p1);                   //If we can output a P object, the output format:  Ref.:110  name:  Dog Doll.        &nbSp Person  p2 = new person (112, "dog left");         System.out.println ("P2:" +P2);          */          person p1 = new person (110, "Simon");         person p2 = new person (110, "Yang over");                //demand: In real life as long as two people's identity card is consistent, then is the same person.         system.out.println ("P1 and P2 are the same objects?") "+ p1.equals (p2));         system.out.println (" P1 hash Code: "+  P1.hashcode ());         system.out.println ("P2 hash Code:" + p2.hashCode () );     }}


This article from "Small Fish Blog" blog, declined reprint!

Java Object Class

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.