Home > Java > javaTutorial > body text

Introduction to value passing and reference passing of java parameters (with examples)

不言
Release: 2018-09-25 14:55:12
Original
2784 people have browsed it

This article brings you an introduction to value passing and reference passing of java parameters (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Value transfer: When the method is called, the actual parameter passes its value to the corresponding formal parameter. Changes in the formal parameter value during method execution do not affect the value of the actual parameter.

Passing by reference: also called passing by address. When a method is called, the reference (address, not 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 parameter is actually the operation on the actual parameter. During method execution Changes in formal parameter values ​​will affect the actual parameter values.

a. Data types for passing values: eight basic data types and String (this can be understood, but in fact String is also the address passed, but the string object is different from other objects, and the string object cannot be If the content changes, a new object will be generated. Then StringBuffer will be fine, but only its content will be changed. The memory address pointed to by the external variable cannot be changed).

b. Data type for passing address value: all composite data types except String, including arrays, classes and interfaces.

Example of value transfer:

package com.other.test;
public class Test {
public static void change(int i, int j) { 
int temp = i; 
i = j; 
j = temp;
} 
public static void main(String[] args) { 
int a = 3; 
int b = 4; 
change(a, b); 
System.out.println("a=" + a); 
System.out.println("b=" + b);
}
}
Copy after login

The output result is a=3 b=4, the passed value will not change the original value

Passed by reference Example: (array)

package com.other.test;
public class Test {
public static void change(int[] counts) { 
counts[0] = 6; 
System.out.println(counts[0]);
} 
public static void main(String[] args) { 
int[] count = { 1, 2, 3, 4, 5 }; 
change(count);
System.out.println(count[0]);
} 
}
Copy after login

The output result is 6 6, that is, the reference value has changed the original value. Example of reference transfer: (object)

Define an A object:

package com.other.test;
public class A {
int i = 0;
}
Copy after login

Classes that operate on the above object:

package com.other.test;
public class Test {
public static void add(A a) { 
//a = new A();   ①
a.i++;
} 
public static void main(String args[]) { 
A a = new A(); 
add(a); 
System.out.println(a.i );
} 
}
Copy after login

When ① is annotated, the output result is 1, when ① is not annotated, the output result is 0, the reason is a =new A(); When a new A object is constructed, it is not the object passed. Look at the situation of String

package com.other.test;
public class Test {
String str = new String("old"); 
char[] ch = { 'a', 'b', 'c' }; 
public static void main(String args[]) { 
Test ex = new Test(); 
ex.change(ex.str, ex.ch); 
System.out.print(ex.str + " and "); 
System.out.println(ex.ch);
} 
public void change(String str, char ch[]) { 
str = "new"; 
ch[0] = 'd';
}
}
Copy after login

The output result is old and dbc, that is, passing String does not change the original value, but creates a new value.

ch[] is a simple array transfer. (The object includes the object reference, that is, the address and the content of the object) String is special. Anyone who has read the String code knows that String is final. So the value is unchanged. The copy of the String object reference in the function points to another new String object, while the copy of the array object reference does not change, but changes the content of the data in the object. ​

For object types, also It is a subclass of Object. If you modify the value of its members in a method, the modification will take effect. After the method call is completed, its members will have new values. However, if you point it to another object, the method After the call is completed, the original reference to it does not point to the new object.

Java parameters, whether they are primitive types or reference types, are passed by copies (there is another way of saying it is by value, but it is easier to understand by saying that it is passed by value. Passing by value is usually is relative to the address).​

If the parameter type is a primitive type, then what is passed is a copy of the parameter, which is the value of the original parameter. This is the same as the value passing discussed before. If the value of the copy is changed in the function, the original value will not be changed. If the parameter type is a reference type, then what is passed is a copy of the reference parameter, and this copy stores the address of the parameter. If the address of this copy is not changed in the function, but the value in the address is changed, then the change within the function will affect the parameters passed in. If the address of the copy is changed in the function, such as new, then the copy points to a new address. At this time, the parameter passed in still points to the original address, so the value of the parameter will not be changed.

The above is the detailed content of Introduction to value passing and reference passing of java parameters (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template