Use of Object objects in Java Learning (toString, Equals, Hashcode) (Small records in Java learning)

Source: Internet
Author: User

The use of Object objects in Java Learning (Small records in Java learning) Wang Coli (Star stars)

Object Objects ( Reference API learning )

Focus on three methods:

1.toString returns the string representation of the object.

2.equals (Object obj) indicates whether another object is equal to this object.

3.hashCode () returns the hash code of the object.

See the code for comment Analysis (one is the person class override method, one is the Star class usage method)

Star Class Code:

1  Packagestudy;2 3  Public classStar {4      Public Static voidMain (string[] args) {5         6         //Creating Object Objects7Object o =NewObject ();8         9         //1.toStringTenString str =o.tostring (); OneSystem.out.println (str);//[email protected] Print the full name of the class + address ASystem.out.println (o);//[email protected] -         //println (object o) Internal implementation should be o.tostring -         //Ctrl + The left mouse button to see the source code, the package is SRC compression package jdk inside the          -         //Requirement: There is a person class with two attributes name, age, and now you want to print the person object directly with name and time.  -Person p =NewPerson ("star", 12); - System.out.println (p); +         //Printing the full name + address of the class above does not make any sense -         //The ToString method is generally overridden in a class.  +          A         //2.equals: Used to compare whether two objects are the same object and actually compare the address values of two objects.  atObject O1 =NewObject (); -Object O2 =NewObject (); -Object O3 =O1; -System.out.println (O1.equals (O2));//false -System.out.println (O1.equals (O3));//true -          inperson P1 =NewPerson ("Little Star", 15); -person P2 =NewPerson ("Little Star", 15); to         //the description is an object, but in memory it is two objects.  +         //demand equals to compare if I describe the same object to print true, otherwise false -         //overriding the Equals method the System.out.println (P1.equals (p2)); *          $         //Api inside when this method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the Hashcode method, where the object that declares equality must have an equal hash codePanax Notoginseng System.out.println (P1.hashcode ()); - System.out.println (P2.hashcode ()); the    } +}

Person Class Code:

1  Packagestudy;2 3 ImportJavax.print.attribute.HashAttributeSet;4 5  Public classPerson {6 String name;7     intAge ;8      PublicPerson (String name,intAge ) {9          This. Name =name;Ten          This. Age =Age ; One     } A      -     //overriding the Tosting method - @Override the      PublicString toString () { -         //TODO auto-generated Method Stub -          -         return"Name =" +name+ "age =" +Age ; +     } -      +      A     //overriding the Equals method at @Override -      Public Booleanequals (Object obj) { -         //TODO auto-generated Method Stub -Person p =NULL; -         if(objinstanceofPerson ) { -             //polymorphic requires strong turn, parent class cannot access attributes unique to subclasses inp =(person) obj; -             if(P.name.equals ( This. Name)) { to                 return true;//The same name is right. +}Else{ -                 return false;//a different name is not an object the             } *}Else{ $         return false;//No, this class is wrong.Panax Notoginseng     } - } the   //rewrite hash code hashcode + @Override A      Public inthashcode () { the          +         return  This. Name.hashcode (); -     } $ } $      

Use of Object objects in Java Learning (toString, Equals, Hashcode) (Small records in Java learning)

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.