Parameter transfer and object clone in Java

Source: Internet
Author: User

1. parameter transfer

We all know that there are two methods for passing parameters: value transfer and reference transfer. In the object-oriented language of Java, how is it? According to the following procedure, we can draw a conclusion:

Class OBJ {
String STR = "init value ";
Public String tostring (){
Return STR;
}
}
Public class objref {
OBJ aobj = new OBJ ();
Int aint = 11;
Public void changeobj (OBJ inobj ){
Inobj. Str = "changed value ";
}
Public void changepri (INT inint ){
Inint = 22;
}
Public static void main (string [] ARGs)
{
Objref oref = new objref ();

System. Out. println ("before call changeobj () method:" + oref. aobj );
Oref. changeobj (oref. aobj );
System. Out. println ("after call changeobj () method:" + oref. aobj );

System. out. println ("========================== print primtive ===================== ");
System. Out. println ("before call changepri () method:" + oref. Aint );
Oref. changepri (oref. Aint );
System. Out. println ("after call changepri () method:" + oref. Aint );

}
}
In the above Code, the parameter passed in by the changeobj (OBJ inobj) method is the reference of the object, and the changepri (INT inint) method is an int value. The output result is as follows:

Before call changeobj () method: init Value
After call changeobj () method: changed Value
======================== Print primtive ==============================
Before call changepri () method: 11
After call changepri () method: 11

Obviously, the former modifies the input parameters, and the latter has no effect on the input parameters. That is to say, when the method parameter is of the basic data type, the input variable becomes the local variable of the method. This local variable is a copy of the input parameter, all internal operations of the function body are for this copy operation. Therefore, the input parameters are not affected after the method is executed. This parameter is passed through the value! When the method parameter is an object, only the object reference is passed. Changes to the object in the method directly affect the input object parameter. This method is passed by reference!

Therefore, it is concluded that when the input parameter is of the basic data type, value transmission is used, and when the input parameter is an object, reference transmission is used.

Ii. Object clone

In Java, if a copy object needs to be exactly the same as an object, it is wrong to assign values directly, because the value assignment operation passes the object reference, the clone method is the most efficient and convenient.

To clone an object, the class to which the object belongs must implement the clonable interface and implement the clone () method. There are two types of clone: Shadow Clone and deep clone.

The effect of calling the clone () method in the object class is: first open up a space in the memory and the original object, and then copy the content in the original object. There is no problem with the basic data types, but for non-basic variables, because they only store object references, in this way, the non-basic type variables in the cloned object and the non-basic type variables in the object before cloning point to the same object, which is similar to the direct assignment between objects mentioned above. This type of clone is called "Shadow Clone ".

Obviously, we want the non-basic type variables in the objects (A, clonea) before and after clone to point to different objects (c), which requires "Deep clone ". To implement "Deep clone", you must implement the clone () method of the object (c) to which non-basic type variables point, and clone () of the clone object (() method. For example

Class C implements clonable {

Private int CINT;

Public C (int ii) {CINT = II ;}

Public void doublevalue (int ii) {CINT * = 2 ;}

Public String tostring () {return integer. tostring (CINT );}

Public object clone (){
C o = NULL;
Try {
O = (c) Super. Clone ();
} Catch (clonenotsupportedexception e ){
E. printstacktrace ();
}
Return O;
}
}

Class A implements clonable {

Private int aint;

C cc = new C (123 );

Public object clone (){
A o = NULL;
Try {
O = (a) Super. Clone ();
} Catch (clonenotsupportedexception e ){
E. printstacktrace ();
}
O. Cc = (c) CC. Clone ();
Return O;
}
Call time:

A aa = new ();

A bb = (a) AA. Clone ();

In this case, the content in the AA and BB objects is exactly the same!

}

 

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.