Dark Horse Programmer-----Object class and wrapper class

Source: Internet
Author: User

/**
* @author Administrator
*
* @description Object class Learning test class
* @history
*/
public class Objecttestdemo {
/**
* @description
* @param args
* @throws ClassNotFoundException
*/
public static void Main (string[] args) throws ClassNotFoundException {
Object class Learning test code
1. ToString Method
2. Equals and Hashcode methods
3. GetClass method
Objecttestdemo OTD = new Objecttestdemo ();
System.out.println (OTD);
System.out.println (Otd.tostring ());//Real call to ToString method
/*
* ToString method in object class
* Public String toString () {
Return GetClass (). GetName () + "@" + integer.tohexstring (Hashcode ());
}
* Equal method in object class
* Public boolean equals (Object obj) {
return (this = = obj);
}*/
Defines the nickname and age attributes, and the equal method returns True if the nickname and age are the same
Objecttestdemo OTD1 = new Objecttestdemo ();
Otd1.nickname = "Eclipse";
Otd1.age = 20;
Objecttestdemo OTD2 = new Objecttestdemo ();

Otd2.nickname = "Eclipse";
Otd2.age = 20;
System.out.println (OTD1==OTD2); False
System.out.println (Otd1.equals (OTD2)); The return is always False if not written.
There is no absolute relationship between the Hashcode method and the Equals method.
Refer to the source code of the note description, in the actual use as far as possible to overwrite both methods, to maintain consistency
Public final native class<?> GetClass ();
The GetClass method is a local method that invokes the underlying code and returns an instance of the class object corresponding to the current object.
Class object instance, which is understood as the corresponding byte-code object in memory, and the Java reflective content is initially learned
Construct an object instance by byte-code object
Class Claz = Otd.getclass ();
Class claz1 = Objecttestdemo.class;
Class claz2 = Class.forName ("Objecttestdemo"); The full path of the class
System.out.println (Otd.getclass ()); Class Objecttestdemo
Only one byte-code object is generated in VM memory
System.out.println (Claz = = CLAZ1); True
}
Method simulation, add two attributes nickname and age
Private String nickname;
private int age;
Note that the parameter type should not be written as Objecttestdemo
public boolean equals (Object obj) {
Objecttestdemo OTD = (objecttestdemo) obj; Transform up, cast
if (this = = OTD) {
return true;
}
if (OTD instanceof Objecttestdemo) {
if (This.nickname.equals (otd.nickname) && this.age = = otd.age) {
return true;
}
}
return false;
}
Public String toString () {
return "HelloWorld"; Overwrite ToString method, return HelloWorld
}
}

Dark Horse Programmer-----Object class and wrapper 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.