Simple understanding of methods in Object classes

Source: Internet
Author: User

When it comes to the toString () method, you have to look at the Object class first.
The Object class is a super class and the parent class of all classes. You can see sun's JDK. In fact, there are not many methods, but they are very practical.
I have always had a mysterious feeling about the Object. Yesterday, my friend asked me about Clone (), but there are still many puzzles;
So today I decided to unveil the Object. The following is my understanding of the Object after reading the document and reading the JDK. If there is any error, please note:
Package java. lang;
Public class Object {}
JDK1.4 contains 11 methods, including clone (), equals (), getClass (), notfiy (), toString (), wait ().......
Here I will focus on clone (), equals (), toString ()
1. clone ()
All classes with the clone function have a feature that directly or indirectly implements the Cloneable interface. Otherwise, when we try to call the clone () method, the CloneNotSupportedException exception will be triggered.
Protected native Object clone () throws CloneNotSupportedException; (in JDK)
It can be seen that it is a protected method, so we cannot simply call it,
Native: the keyword native, indicating that this method is implemented in a language other than java. The rhetorical method does not include implementation (refer:Http://blog.csdn.net/mingjava/archive/2004/11/14/180946.aspx)
Here I will not specifically talk about how clone () implements the Cloneable interface and how to overwrite it; this will talk about the role and usage of clone;
For object x,
X. clone ()! = X
X. clone (). getClass () = x. getClass ()
X. clone (). equals (x)
X. clone (). getClass () = x. getClass ()
The values returned above are true.
2 equals ()
Its prototype is:
Public boolean equals (Object obj ){
Return (this = obj );
}
The equals (Object) defined in the Object is a comparative reference, which works the same as "=.
3. toString ()Its prototype is:
Public String toString (){
Return getClass (). getName () + "@" + Integer. toHexString (hashCode ());
}
The toString () method is the most common and overwrites it in many classes:
Let's take a look at two examples.
Example 1:
Class Example {
Private String s = "abc ";
Public Example (String str ){
This. s = str + this. s;
}
Public static void main (String [] args ){
Example ex = new Example ("123 ");
System. out. println (ex );
System. out. println (ex. s );
}
Public String toString (){
This. s = "CBA" + this. s;
Return s;
}
Output result:
Cba123abc
Cba123abc
Example 2.
Class Example {
Private String s = "abc ";
Public Example (String str ){
This. s = str + this. s;
}
Public static void main (String [] args ){
Example ex = new Example ("123 ");
System. out. println (ex );
System. out. println (ex. s );
}
Public String toString2(){
This. s = "CBA" + this. s;
Return s;
}
Output result:
Example @ 1858610
123abc
Example 1: With example 2, only the tostring method name is modified, but the execution is quite different;
Example 1: tostring () is overwritten. When new Example ("123"); is executed, the constructor (not executed for the moment) is called before loading the method tostring () in the execution parent class () (if it is overwritten here, the method code in this class will be run.) Finally, the constructor will execute the methods. The execution sequence is involved here, which is not discussed. out. println (ex); is used to print the string value returned by the tostring () method;
In Example 2: tostring () is not overwritten.2() Is a common method in the class, so when new Example ("123");, the toString2(), So the s value is "123abc", and System. out. println (ex); the printed result is the hash code of the object indicated by the class name + @ + hexadecimal number in the prototype.
The tostring () method is still used many times. When writing classes, we 'd better have our own tostring to overwrite the prototype;

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.