The difference between value passing and reference passing in Java

Source: Internet
Author: User

Value passing: (formal parameter type is the basic data type): When the method is called, the actual parameter passes its value to the corresponding formal parameter, the formal parameter only initializes its own storage unit content with the value of the actual parameter, it is two different storage units, so the change of the formal parameter value in the method execution does not affect the value of the actual parameter.

Reference passing: (formal parameter type is a reference data type parameter): Also known as a pass-through address. Method invocation, the actual parameter is an object (or an array), then the actual parameters and formal parameters point to the same address, in the execution of the method, the operation of the formal parameters is actually the operation of the actual parameters, the result is preserved after the end of the method, so the change in the method execution parameters will affect the actual parameters.


Test Summary: Value transfer is a copy of the value, including the basic type, such as int, long, etc., also includes the basic variable type wrapper class string, long, integer, etc., although the class but the use of value passing (tested) reference passing is the copy of the address, common objects, etc.

http://464772913.iteye.com/blog/1483163 the concepts of "formal parameters" and "actual parameters" must be understood before Java value passing and reference passing

In a method, such as method1 (Object o), O is the formal parameter, formal parameters, when you call this method, to pass in a value, this value is called the actual parameter, also called the argument, the parameter you can understand as a placeholder (for the incoming argument occupies a position).

Value passing: When a method is called, the actual parameter passes its value to the corresponding formal parameter, and the change of the formal parameter value in the method execution does not affect the value of the actual parameter.
Reference delivery: Also known as a pass-through address. When a method is called, the reference to the actual parameter (the address, not the value of the parameter) is passed to the corresponding formal parameter in the method, and in the execution of the method, the operation of the formal parameter is actually the operation of the actual parameter, and the change of the parameter value in the execution of the method will affect the value of the actual parameter.
The following examples illustrate:
Pass-through ---Basic data type parameters
public class passvalue{
static void Exchange (int a, int b) {//static method, interchange A, b
int temp;
temp = A;
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
Exchange (I, j); Value is passed, the main method can only call static methods
System.out.println ("After call:" + "i=" + i + "\ T" + "J =" + j);//After invocation
}
}
Operation Result:
Before call:i = ten j = 100
After call:i = ten j = 100
Note: When you call Exchange (I, j), the actual parameter i,j the value to the corresponding formal parameter, a, B, when the method Exchange () is executed, the change in the value of the formal parameter, a, B, does not affect the values of the actual parameters I and J, and the values of I and J do not change before or after the call
reference to pass---object as a parameter
When a method invokes an object (or array) as an argument in a method, the argument passes the reference (address) of the object, that is, when the method is called, the actual parameter passes the reference (address) of the object to the formal parameter. This is the actual parameter and the form parameter point to the same address, that is, the same object (array), when the method executes, the change of the formal parameter is actually a change to the actual parameters, the result is preserved after the call is over.
Class book{
String name;
Private Folat Price;
Book (String n, float) {//constructor method
name = N;
Price = P;
}
static void Change (book A_book, String N, float p) {//static method, object as parameter
A_book.name = n;
A_book.price = p;
}
public void output () {//instance method, output 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); Reference passing, passing the reference to object B, modifying the value of object B
System.out.print ("after Call:\t"); After the call
B.output ();
}
}
Operation Result:
Before Call:name:java2 price:32.5
After call:name:c++ price:45.5
Note: When you call change (b, "C + +", 45.5f), object B is used as the actual parameter, the reference is passed to the corresponding formal parameter a_book, in fact A_book also points to the same object, that is, the object has two reference names: B and A_book. When you execute a method change (), the A_book operation on the formal parameter is the operation of the actual parameter B.

More articles:

The difference between Java value passing and reference passing

The difference between value passing and reference passing in Java

Related Article

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.