Object: is the root class of all classes. All classes are directly or indirectly inherited from the class.
1
Create Student objects print the address value of the student object (essentially a hash value that is calculated from the address through the hash algorithm)
The hash value of the same object must be the same.
An object with the same hash value may not have the same address value.
Xinhua dictionary, Zhu--456 (hash)--pig (third), Zhu (5th), beads (9th)
2
Why override the ToString () method
By default, a string representation of an object is printed, with little meaning.
Package name ... class name [email protected]+ the hexadecimal value of the object's hash
Therefore, we recommend that all subclasses override the method.
Automatically generated.
Represents the data for this object:
[Class name member Variable 1 = data value 1, member variable 2 = data value 2,...]
1 classstudent{2 //name and age3 PrivateString name;4 Private intAge ;5 //Auto-generated shortcut keys Shift+alt+s6 PublicString GetName () {7 returnname;8 }9 Public voidsetName (String name) {Ten This. Name =name; One } A Public intGetage () { - returnAge ; - } the Public voidSetage (intAge ) { - This. Age =Age ; - } - PublicStudent (String name,intAge ) { + Super(); - This. Name =name; + This. Age =Age ; A } at PublicStudent () { - Super(); - } - //override the ToString method in the object class to automatically generate - - PublicString toString () { in return"Student [name=" + name + ", age=" + Age + "]"; - } to + } - the * Public classStudentdemo { $ Public Static voidMain (string[] args) {Panax Notoginseng //Create student objects and print student objects -Student S1 =NewStudent ("Lin Chi-ling", 50); theStudent s2 =NewStudent ("Andy Lau", 55); + A //print two student objects theSystem.out.println (S1);//[email protected] +SYSTEM.OUT.PRINTLN (S2);//[email protected] - /** $ * [email protected], we previously called him the address value is actually not accurate, $ * Because at that time to let everyone understand, we will call him the address value. - * Note: He was in fact a string representation of an object-format: Package name + class name [email protected]+ the object's hash code value - * the * What is a hash value? - * There is a hashing algorithm that can calculate the hash value of the object, the hash value of the same object is the same,Wuyi * But remember, the hash value doesn't mean it's the same object . the * - * Example: Xinhua dictionary Wu * Zhu 465 pages (hash value) Pig (1th) Live (fourth) wish (sixth) - */ About}overriding the ToString () method
The results of the operation are as follows:
Student [Name= Lin Chi-ling, age=50]
Student [Name= Andy Lau, age=55]
3
Equals ():
By default, the address value is compared. There is no point.
Therefore, it is recommended that subclasses override this method.
Automatically generated.
This generated method works by comparing the values of the member variables of the object.
1 classstudent2{2 PrivateString name;3 Private intAge ;4 PublicStudent2 (String name,intAge ) {5 Super();6 This. Name =name;7 This. Age =Age ;8 }9 PublicStudent2 () {Ten Super(); One //TODO auto-generated Constructor stub A } - PublicString GetName () { - returnname; the } - Public voidsetName (String name) { - This. Name =name; - } + Public intGetage () { - returnAge ; + } A Public voidSetage (intAge ) { at This. Age =Age ; - } - - Public inthashcode () { - Final intPrime = 31; - intresult = 1; inresult = Prime * result +Age ; -result = Prime * result + ((name = =NULL) ? 0: Name.hashcode ()); to returnresult; + } - the //The Equals () method compares the values of the member variables of an object, and equals () when an object and the member variable value of another object are exactly the same. * //You can think of these two objects as if they were the same object $ Panax Notoginseng Public Booleanequals (Object obj) { - if( This==obj) the return true; + if(obj = =NULL) A return false; the if(GetClass ()! =Obj.getclass ()) + return false; -Student2 other =(Student2) obj; $ if(Age! =other.age) $ return false; - if(Name = =NULL) { - if(Other.name! =NULL) the return false; -}Else if(!name.equals (other.name))Wuyi return false; the return true; - } Wu } - About $ Public classObjectdemo { - Public Static voidMain (string[] args) { - //Create two student objects -Student2 S1 =NewStudent2 (); AStudent2 s2 =NewStudent2 (); + //The Equals () method is a method in the object class, and the default is to compare the address values of the objects the System.out.println (s1.equals (S2)); -System.out.println (S1==S2);//= = The address value can be compared completely $ the //Create two student objects theStudent2 s3 =NewStudent2 ("Andy Lau", 55); theStudent2 S4 =NewStudent2 ("Andy Lau", 55); the System.out.println (S3.equals (S4)); - in}The Equals () method demonstrates
The results of the operation are as follows:
True
False
True
Java Learning: Object class