The first season of Java (object class)

Source: Internet
Author: User

Package cn.itcast_01;/* * Object: Class object is the root class of a class hierarchy. Each class uses Object as a superclass. * Each class is inherited directly or indirectly from the object class. * * Method of the object class: * public int hashcode (): Returns the hash code value of the object. Generally, different objects have different hash code values. * 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 the value of the address. * *public Final Class getclass (): Returns the run-time class for this Object, which class is running, and the full name of the class is returned when the GetName () method is called. Method of the *class class: *public string GetName (): Returns the entity represented by this class object as a String * returns: the class or interface name represented by this object. */public class Studenttest {public static void main (string[] args) {Student S1 = new Student (); System.out.println (S1.hashcode ()); 11299397Student s2 = new Student (); System.out.println (S2.hashcode ());//24446859Student s3 = S1; System.out.println (S3.hashcode ()); 11299397system.out.println ("-----------"); Student s = new Student (); Class C = S.getclass (); String str = c.getname (); System.out.println (str); cn.itcast_01.student//Chained programming String str2 = S.getclass (). GetName (); System.out.println (STR2);}}

Method of the object class: public string ToString (): Returns the string representation of the object.

The value of the ToString () method of the object class is equivalent to it getclass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())

But half of the custom class overrides the Tistring method, otherwise the output calls ToString in the object class by default. How to rewrite? Automatically generated.

Attention:
* directly outputting the name of an object is actually called the ToString () method of the object. Here you can see if the class has overridden the ToString method of the object class.
* If the result is in the form of an address, it means that the ToString method has not been rewritten.

/*
* Method: 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 generally does not make much sense, so override this method.
* How to rewrite it?
*are generally used to compare the values of the member variables of an object.
*In fact, it is automatically generated.
*
* See source code:
*public boolean equals (Object obj) {
*this-s1//who calls this method first will represent who
*Obj-s2
*return (this = = obj);
*}
*
* ==:
*Base type: The comparison is whether the value is the same
*Reference type: The comparison is whether the address value is the same
* Equals:
*Reference type: By default, the address value is compared.
*However, we can rewrite the method as appropriate. General overrides are automatically generated, comparing the values of the member variables of the object
*/

Code Demo:

public class Studentdemo {public static void main (string[] args) {Student S1 = new Student ("Brigitte", 27); Student s2 = new Student ("Brigitte", 27); System.out.println (S1 = = s2); Falsestudent s3 = S1; System.out.println (S1 = = s3);//TrueSystem.out.println ("---------------"); System.out.println (s1.equals (S2)); obj = s2; TrueSystem.out.println (S1.equals (S1)); TrueSystem.out.println (S1.equals (S3)); Truestudent S4 = new Student ("Wind Qingyang", 30); System.out.println (S1.equals (S4)); Falsedemo d = new Demo (); System.out.println (S1.equals (d)); Classcastexception}}class Demo {}
Note: the Equals () method is overridden in the student class. The rewritten code is as follows:

Override the Equals method in the student class.
@Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ()) return false; Student other = (Student) obj;if (age! = other.age) return false;if (name = = null) {if (other.name! = null) return false;} E LSE if (!name.equals (other.name)) return False;return true;}




The first season of 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.