Java common objects individual methods in the object class

Source: Internet
Author: User
Tags stub

Java common Objects Object class

public int hashcode (): Returns the hash code value of the object.

Note: The hash value is a value computed from the hash algorithm, which is related to the address value, but not the actual address value. You can understand it as an address value.

 PackageCommon_object; Public classDemoextendsobject{} PackageCommon_object; Public classDemotest { Public Static voidMain (string[] args) {Demo d=NewDemo ();   System.out.println (D.hashcode ()); //1887400018Demo D2 =NewDemo ();  System.out.println (D2.hashcode ()); //285377351    }}

Public final Class GetClass (): Returns the run-time class for this object

Class method: public string GetName (): Returns the entity represented by this class object as a string.

 PackageCommon_object; Public classDemoextendsobject{} PackageCommon_object; Public classDemotest { Public Static voidMain (string[] args) {Demo d=NewDemo (); Class C=D.getclass ();  System.out.println (C.getname ()); //Common_object. DemoString str =NewDemo (). GetClass (). GetName ();  System.out.println (str); //Common_object. Demo    }}

public string toString (): Returns the string representation of the object.

static method under the integer class: public static string tohexstring (int i): Turns an integer into a hexadecimal-represented string.

 PackageCommon_object; Public classDemoextendsobject{} PackageCommon_object; Public classDemotest { Public Static voidMain (string[] args) {Demo d=NewDemo ();   System.out.println (D.hashcode ()); //1887400018System.out.println (D.getclass (). GetName ());//Common_object. DemoSystem.out.println ("-----------------------");   System.out.println (D.tostring ()); //[email protected]//the value of the ToString () method is equivalent to//getclass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())//is equal to//This.getclass (). GetName () + ' @ ' + integer.tohexstring (This.hashcode ())System.out.println ("-----------------------"); System.out.println (D.getclass (). GetName ()+ ' @ ' +integer.tohexstring (D.hashcode ()));        System.out.println (D.tostring ()); //[email protected]//[email protected]    }}

The composition of this information is explained, but this information is meaningless, so it is recommended that all subclasses rewrite the method.

Make a return of the values of all member variables of the class. The final version of the rewrite scenario is to automatically generate the ToString () method.

Note: outputting the name of an object directly is the ToString () function that invokes the object.

How to automatically generate the ToString () method. -------. Java program Interface, right click, select Source (Alt + Shift + S), select Generate ToString ... You can do it.

//override the ToString () method.  PackageCommon_object; Public classDemoextendsobject{PrivateString name; Private intAge ;  PublicDemo () {Super(); //TODO auto-generated Constructor stub    }     PublicDemo (String name,intAge ) {        Super();  This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; } @Override PublicString toString () {return"Demo [name=" + name + ", age=" + Age + "]";//This is the override of the ToString () method.     }    } PackageCommon_object; Public classDemotest { Public Static voidMain (string[] args) {Demo d=NewDemo ();   System.out.println (D.hashcode ()); //1887400018System.out.println (D.getclass (). GetName ());//Common_object. DemoSystem.out.println ("-----------------------");   System.out.println (D.tostring ()); //Demo [Name=null, age=0] change here//the value of the ToString () method is equivalent to//getclass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())//is equal to//This.getclass (). GetName () + ' @ ' + integer.tohexstring (This.hashcode ())System.out.println ("-----------------------"); System.out.println (D.getclass (). GetName ()+ ' @ ' +integer.tohexstring (D.hashcode ()));        System.out.println (D.tostring ()); //[email protected]//Demo [Name=null, age=0] There's also a change here.    }}

public boolean equals (Object obj): Indicates whether another object is "equal" to this object.

This method, by default, compares the address value. Comparing address values is generally not very meaningful, so we have to rewrite this function again.

are generally used to compare the values of object member variables.

Overriding the final version can be generated automatically with eclipse. -------. The Java program interface, right-click, select Source (Alt + Shift + S), select Generate Hashcode () and Equals () ... You can do it.

 PackageCommon_object; Public classDemoextendsobject{PrivateString name; Private intAge ;  PublicDemo () {Super(); //TODO auto-generated Constructor stub    }     PublicDemo (String name,intAge ) {        Super();  This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; } @Override Public inthashcode () {Final intPrime = 31; intresult = 1; Result= Prime * result +Age ; Result= Prime * result + ((name = =NULL) ? 0: Name.hashcode ()); returnresult; } @Override Public Booleanequals (Object obj) {if( This==obj)return true; if(obj = =NULL)            return false; if(GetClass ()! =Obj.getclass ())//another way to express it://Format: Object name instanceof class name//indicates whether the object name is an object of the class name//if (! ( obj instanceof Demo))//return false;            return false; Demo Other=(Demo) obj; if(Age! =other.age)return false; if(Name = =NULL) {            if(Other.name! =NULL)                return false; } Else if(!name.equals (other.name))return false; return true; }} PackageCommon_object; Public classDemotest { Public Static voidMain (string[] args) {Demo D1=NewDemo ("White", 21); Demo D2=NewDemo ("Black", 20);  System.out.println (D1.equals (D2)); //false    }}

Java common objects individual methods in the object class

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.