Pass by reference or pass by value in Java

[1: Understand the difference between basic types and reference types]

int num = 10;
String str = "hello";

write picture description here
As shown in the figure, num is a basic type, and the value is directly stored in the variable. And str is a reference type, and what is stored in the variable is only the address of the actual object. This kind of variable is generally called "reference", the reference points to the actual object, and the actual object stores the content.
[Two: Understand the role of the assignment operator (=)]

num = 20;
str = "java";

write picture description here

For the primitive type num, the assignment operator will directly change the value of the variable, overwriting the original value. For the reference type str, the assignment operator will change the address stored in the reference, and the original address will be overwritten. But the original object is not changed (important). As shown above, the "hello" string object has not been changed. (objects not pointed to by any reference are garbage and will be collected by the garbage collector)

[Three: What happened when the method was called? Parameter passing is basically an assignment operation.

//第一个例子:基本类型
void foo(int value) {
    value = 100;
}
foo(num); // num 没有被改变

//第二个例子:没有提供改变自身方法的引用类型
void foo(String text) {
    text = "windows";
}
foo(str); // str 也没有被改变

//第三个例子:提供了改变自身方法的引用类型
StringBuilder sb = new StringBuilder("iphone");
void foo(StringBuilder builder) {
    builder.append("4");
}
foo(sb); // sb 被改变了,变成了"iphone4"。

//第四个例子:提供了改变自身方法的引用类型,但是不使用,而是使用赋值运算符。
StringBuilder sb = new StringBuilder("iphone");
void foo(StringBuilder builder) {
    builder = new StringBuilder("ipad");
}
foo(sb); // sb 没有被改变,还是 "iphone"。

Focus on understanding why, the third example and the fourth example have different results?
Here's an illustration of the third example:
write picture description here
after builder.append("4")
write picture description here

Here's an illustration of the fourth example:
write picture description here
builder = new StringBuilder("ipad"); after
write picture description here


  1. = is an assignment operation (anything that contains =, such as +=, -=, /=, etc., has an implicit assignment operation). It's no longer the mathematical meaning you used to understand, and + - */ and = are even more not a level in java, in other words, = is an action, an operation that can change the state of memory, a symbol that can change a variable, And +-*/ doesn't. The assignment operation here actually contains two meanings: 1. Give up the original value or reference; 2. Get the value or reference of the variable on the right side of = . The understanding of = in Java is very important! ! It's a pity that many people ignore it, or understand it but don't think about it.
  2. For primitive data type variables, the = operation completely copies the value of the variable . In other words, "After =, you and I are no longer related"; as for basic data types, it is not covered here.
  3. For non-primitive data type variables, the = operation copies the variable's reference . In other words, "Hey, the variable on the left side of the =, don't fiddle with me! We're grasshoppers on a rope now, unless you get = again and drop the existing reference!! It's said above! = is an action, so I use = as a verb!!”.
  4. The parameter itself is a variable, and parameter passing is essentially an = operation . Parameters are variables, and all our operations on variables, the behaviors that variables can have, have parameters. So forget about the theory that parameters are passed by value and pointers in the C language, and parameter passing is the = operation.

Java has only one way of passing parameters: that is pass-by-value, that is, passing anything in Java is pass-by-value.
If the parameter is a primitive type, a copy of the literal value of the primitive type is passed.
If the parameter is a reference type, a copy of the address value in the heap of the object referenced by the parameter is passed

[Excerpted from Zhihu Q&A] Is
linking Java pass-by-value or pass-by-reference?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325448776&siteId=291194637