Common methods of object in Java

Source: Internet
Author: User

1.clone ()

Clone ()                throws Clonenotsupportedexception
creates and returns a copy of this object. The exact meaning of a "copy" may depend on the class of the object.

1> Clone&copy
     Assuming there is now an employee object, employee Tobby =new employee ("Cmtobby", 5000), pass
Often we will have such assignment employee cindyelf=tobby, this time just simple copy a bit reference,cindyelf and tobby all point to the same object in memory, Such a cindyelf or tobby operation can affect each other. For example, if we change the value of the salary field through the Cindyelf.raisesalary () method, then tobby through the Getsalary () method is the value of the modified Salary field, which is obviously not what we would like to see. We want to get an exact copy of the Tobby, while the two do not affect each other, and we can use clone to meet our needs. Employee Cindy=tobby.clone (), a new employee object is generated and has the same property values and methods as Tobby.
      2>. How is shallow clone&deep clone
clone done? object is not ignorant about an object when it is being implemented, it simply executes the domain-to-domain copy, which is shallow Clone. So, the problem is, in the case of employee, it has a domain hireday is not a basic type of variable, but a reference variable, after cloning will produce a new date type of reference, It points to the same date object as the corresponding field in the original object, so that the clone class shares a portion of the information with the original class, which is obviously unfavorable, as shown in the procedure:

At this point we need to do deep clone, special processing of those non-basic domains, such as the hireday in this example. We can redefine the Clone method and do special processing for hireday, as shown in the following code:

[Java]View Plaincopyprint?
  1.      class  EMPLOYEE  implements  cloneable  
  2. {
  3.         public  object clone ()   throws  clonenotsupportedexception   
  4.         {  
  5.          employee cloned =   (Employee)  super .clone ();   
  6.       cloned.hireday =  (Date)   Hireday.clone ()   
  7.       return  CLONED;  
  8. }
  9. }

3>. Protection mechanism of Clone () method

In Object Clone () is declared as protected, and doing so has some justification to the employee

Class as an example, through the declaration as protected, you can ensure that only the employee class inside can "clone" employee object, the principle can refer to my previous about public, protected, private study notes.

4>. Use of the Clone () method

The use of the Clone () method is relatively simple, note the following points:

A when to use shallow clone, when to use deep clone, this main look at the specific object of the domain is what nature, basic type or reference variable

b The class to which the object calling the Clone () method belongs must implements the Clonable interface, or clonenotsupportedexception is thrown when the Clone method is called.


2.equals ()

3.finalize ()
This method is called by the object's garbage collector when the garbage collector determines that there are no more references to the object.
4.getclass ()
returns the Object run-time class for this.
5.hashcode ()
returns the hash code value of the object.
6.notify ()
wakes up a single thread waiting on this object monitor.
7.notifyAll ()
wakes all the threads waiting on this object monitor.
8.toString ()
toString ()
Returns the string representation of the object. Typically, the toString method returns a string that represents this object as text. The result should be a concise but easy-to-read information expression. It is recommended that all subclasses override this method.

ObjectThe method of the class toString returns a string consisting of the class name (the object is an instance of the class), the at marker " @ ", and an unsigned hexadecimal representation of the hash code for this object. In other words, the method returns a string whose value is equal to:

 
    

9.wait ()























Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Common methods of object in Java

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.