Java value transfer and reference transfer (with examples)
Tips before viewing:
The IDEA version used in this article is ultimate 2019.1, and the JDK version is 1.8.0_141.
1. Data type
1.1 Basic data types
Basic data types include numeric (integer and floating point), character and Boolean
- Integer type: byte, short, int, long
- Floating point: float, double
- Character type: char
- Boolean: boolean
The following program code shows the number of bits, packing type, maximum value, and minimum value of these basic data types.
package testBasicDataType;
public class Test {
public static void main(String[] args) {
//byte
System.out.println("数据类型为 :" + Byte.TYPE);
System.out.println("二进制位数 :" + Byte.SIZE);
System.out.println("包装类为 :" + Byte.class);
System.out.println("最大值为 :" + Byte.MAX_VALUE);
System.out.println("最小值为 :" + Byte.MIN_VALUE);
System.out.println("\n");
//short
System.out.println("数据类型为 :" + Short.TYPE);
System.out.println("二进制位数 :" + Short.SIZE);
System.out.println("包装类为 :" + Short.class);
System.out.println("最大值为 :" + Short.MAX_VALUE);
System.out.println("最小值为 :" + Short.MIN_VALUE);
System.out.println("\n");
//int
System.out.println("数据类型为 :" + Integer.TYPE);
System.out.println("二进制位数 :" + Integer.SIZE);
System.out.println("包装类为 :" + Integer.class);
System.out.println("最大值为 :" + Integer.MAX_VALUE);
System.out.println("最小值为 :" + Integer.MIN_VALUE);
System.out.println("\n");
//long
System.out.println("数据类型为 :" + Long.TYPE);
System.out.println("二进制位数 :" + Long.SIZE);
System.out.println("包装类为 :" + Long.class);
System.out.println("最大值为 :" + Long.MAX_VALUE);
System.out.println("最小值为 :" + Long.MIN_VALUE);
System.out.println("\n");
//float
System.out.println("数据类型为 :" + Float.TYPE);
System.out.println("二进制位数 :" + Float.SIZE);
System.out.println("包装类为 :" + Float.class);
System.out.println("最大值为 :" + Float.MAX_VALUE);
System.out.println("最小值为 :" + Float.MIN_VALUE);
System.out.println("\n");
//Double
System.out.println("数据类型为 :" + Double.TYPE);
System.out.println("二进制位数 :" + Double.SIZE);
System.out.println("包装类为 :" + Double.class);
System.out.println("最大值为 :" + Double.MAX_VALUE);
System.out.println("最小值为 :" + Double.MIN_VALUE);
System.out.println("\n");
//char
System.out.println("数据类型为 :" + Character.TYPE);
System.out.println("二进制位数 :" + Character.SIZE);
System.out.println("包装类为 :" + Character.class);
System.out.println("最大值为 :" + (int) Character.MAX_VALUE);
System.out.println("最小值为 :" + (int) Character.MIN_VALUE);
System.out.println("\n");
//boolean
System.out.println("数据类型为 :" + Boolean.TYPE);
System.out.println("包装类为 :" + Boolean.class);
System.out.println("真值为 :" + Boolean.TRUE);
System.out.println("假值为 :" + Boolean.FALSE);
}
}
The results are as follows
1.2 Reference data types
Including class, interface type, array type, enumeration type, annotation type, string type
2. Formal participation actual parameters
2.1 Use in functions
-
Formal parameters appear in the function definition and can be used in the entire function body, but cannot be used outside the function.
-
The actual parameter appears in the calling function. After entering the called function, the actual parameter variable cannot be used.
2.2 Call
-
The formal parameter variable allocates the memory unit only when it is called, and releases the allocated memory unit immediately at the end of the call. Therefore, the formal parameters are only valid inside the function. After the function call ends and returns to the main calling function, the formal parameter variable can no longer be used.
-
Actual parameters can be constants, variables, expressions, functions, etc., no matter what type of quantity the actual parameters are, they must have certain values when calling a function, so that these values can be transferred to the formal parameters. Therefore, you should use assignment, input and other methods in advance to obtain a certain value for the parameter.
3. Pass by value and pass by reference
3.1 Definition
-
Passing
by value When a method is called, the passed parameters are passed by value copy. -
Pass
by reference When calling a method, the passed parameters are passed by reference. In fact, the address of the passed reference is the address of the memory space corresponding to the variable.
3.2 Examples
Example 1
package testTransfer;
public class Test {
public static void main(String[] args) {
int num = 10;
num = 100;
System.out.println(num);
String str = "ab";
str = "bd";
System.out.println(str);
}
}
The results of the operation are as follows, and the
analysis is as follows
The basic data type is direct assignment, and the reference type is to open up a new address and assign the new address back.
Example 2
package testTransfer;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
int num = 10;
System.out.println("num改变前:" + num);
changeNum(10);
System.out.println("num改变后:" + num);
String str = "ab";
System.out.println("str改变前:" + str);
changeStr(str);
System.out.println("str改变后:" + str);
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
System.out.println("list.get(1)改变前:" + list.get(1));
changeList(list);
System.out.println("list.get(1)改变后:" + list.get(1));
String[] strings = new String[]{
"a", "b"};
System.out.println("strings[1]改变前:" + strings[1]);
changeStrArray(strings);
System.out.println("strings[1]改变后:" + strings[1]);
User user = new User();
user.name = "张三";
user.age = 30;
System.out.println("user.name改变前:" + user.name);
System.out.println("user.age改变前:" + user.age);
changeUserName(user);
changeUserAge(user);
System.out.println("user.name改变后:" + user.name);
System.out.println("user.age改变后:" + user.age);
}
private static void changeNum(int num){
num = 100;
}
private static void changeStr(String str){
str = "abcd";
}
private static void changeList(List<String> list){
list.set(1,"cd");
}
private static void changeStrArray(String[] strings){
strings[1] = "cd";
}
private static void changeUserName(User user){
user.name = "李四";
}
private static void changeUserAge(User user){
user.age = 31;
}
}
class User{
String name;
int age;
}
The results of the operation are as follows, and the
analysis is as follows
-
The value passed by the basic data type parameter is a copy of this actual value, so no matter what other values are assigned to this copy, the original value will not be changed.
-
For reference types (except String), what is passed is a copy of the reference address of the stored value (that is, the reference before and after the transfer is the same), changing the value pointed to by the reference address of this copy will change the content of the original value.
3.3 Conclusion
-
The operations of basic data types are all value transfer. Since the original content and the copy both store actual values and are in different stack areas, the operation of the formal parameters does not affect the original content.
-
Basically (except for String) operations on reference type data are "pass by reference" (passing the referenced address). There are two cases. One is that the formal parameter and the actual parameter keep pointing to the same object address, and the operation of the formal parameter will affect the content of the object pointed to by the actual parameter. One is that the formal parameter is changed to point to the new object address (such as reassignment reference), then the operation of the formal parameter will not affect the content of the object pointed to by the actual parameter.