Classic parsing of value transfer and reference transfer in Java

Source: Internet
Author: User

Value Transfer: When a method is called, the actual parameter passes its value to the corresponding formal parameter. Changing the value of the formal parameter during method execution does not affect the value of the actual parameter.
Reference transfer: Also known as transfer address. When a method is called, the reference (address rather than the value of the parameter) of the actual parameter is passed to the corresponding formal parameter in the method. During method execution, the operation on the formal parameters is actually the operation on the actual parameters. Changing the value of the formal parameter during the execution of the method will affect the actual value of the parameter.
The following is an example:
Pass value --- Pass basic data type parameters
Public class passvalue {
Static void Exchange (int A, int B) {// static method, exchanging values of A and B
Int temp;
Temp =;
A = B;
B = temp;
}
Public static void main (string [] ARGs ){
Int I = 10;
Int J = 100;
System. Out. println ("before call:" + "I =" + I + "/t" + "J =" + J); // before calling
Exchange (I, j); // value transfer. The main method can only call static methods.
System. Out. println ("after call:" + "I =" + I + "/t" + "J =" + J); // after the call
}
}
Running result:
Before call: I = 10 J = 100
After call: I = 10 J = 100.
Note: When you call exchange (I, j), the actual parameters I and j respectively pass the values to the corresponding form parameters A and B. When you execute the Exchange () method, the changes in the values of the formal parameters a and B do not affect the actual values of the I and j parameters. The values of the I and j do not change before and after the call.
Reference transfer --- object as a parameter
If an object (OR array) is used as a parameter in a method, when a method is called, the parameter passes the object reference (address), that is, when the method is called, the actual parameters pass the reference (address) of the object to the form parameter. This is the actual parameter and the form parameter pointing to the same address, that is, the same object (array). During method execution, changes to the form parameter are actually changes to the actual parameter, this result is retained after the call is completed.
Class book {
String name;
Private folat price;
Book (string N, float) {// Constructor
Name = N;
Price = P;
}
Static void change (Book a_book, string N, float p) {// static method, with the object as the parameter
A_book.name = N;
A_book.price = P;
}
Public void output () {// instance method, which outputs Object Information
System. Out. println ("name:" + name + "/t" + "Price:" + price );
}
}
Public class passaddr {
Public static void main (string [] ARGs ){
Book B = New Book ("Java2", 32.5f );
System. Out. Print ("before call:/t"); // before calling
B. Output ();
B. Change (B, "C ++", 45.5f); // transmits the reference of object B and modifies the value of object B.
System. Out. Print ("after call:/t"); // after the call
B. Output ();
}
}
Running result:
Before call: Name: Java2 price: 32.5
After call: Name: C ++ price: 45.5
Note: When change (B, "C ++", 45.5f) is called, object B is used as the actual parameter and the reference is passed to the corresponding format parameter a_book, in fact, a_book also points to the same object, that is, this object has two reference names: B and a_book. When the method change () is executed, the_book operation of the formal parameter is the operation of the actual parameter B.

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.