Parameter passing in java - pass by value, pass by reference

 

 

 

The fact that parameters are passed by value rather than by reference explains that the only mechanism for passing parameters that Java applications have is pass-by-value.

Objects are never passed in a Java application, only object references. Hence the object is passed by reference. The fact that Java applications pass objects by reference does not imply that Java applications pass parameters by reference. Parameters can be object references, and Java applications pass object references by value.

 

Variables in a Java application can be of one of two types: a reference type or a primitive type. Both types are handled the same way when passed as a parameter to a method. Both types are passed by value; none are passed by reference.

 

Pass-by-value and pass-by-reference. Pass-by-value means that when an argument is passed to a function, the function receives a copy of the original value. Therefore, if the function modifies the parameter, only the copy is changed, while the original value remains the same. Pass-by-reference means that when an argument is passed to a function, the function receives the memory address of the original value, not a copy of the value. Therefore, if the function modifies the parameter, the original value in the calling code also changes.

1. Objects are passed by reference
2. Java applications have one and only one parameter passing mechanism, which is pass-by-value
3. Pass-by-value means that when an argument is passed to a function, the function receives the original A copy of the value
4. Pass by reference means that when an argument is passed to a function, the function receives the memory address of the original value, not a copy of the value

First of all, let's take a look at the first point: objects are passed by reference.
  Indeed, I don't think you have any doubts about this, for example:
  class Test01
  {
  public static void main(String[] args)
  {
  StringBuffer s= new StringBuffer(" good");
  StringBuffer s2=s;
  s2.append(" afternoon.");
  System.out.println(s);
  }
  }
  Objects s ​​and s2 point to the same address in memory and therefore point to the same object .
  How to explain "objects are passed by reference"?
  The meaning here is that the object assignment operation is to pass the reference of the object, so the object is passed by reference, is there a problem?
  The output of the program is:
  good afternoon.
  This shows that s2 and s are the same object.
  One thing to clarify here is that passing an object here is actually passing a value, because an object is a pointer, and this assignment is an assignment between pointers, so it is called pass-by-reference in java. (What is a reference? Isn't it an address? An address is nothing but an integer value)
  Look at the following example again:
  class Test02
  {
  public static void main(String[] args)
  {
  int i=5;
  int i2=i;
  i2=6;
  System.out.println(i);
  }
  }
  What is the result of the program? 5! ! !
  What does this mean? Primitive data types are passed by value, and this pass by value also refers to the behavior of assignments. Next question: Java applications have only one parameter passing mechanism, which is pass by value
  class Test03
  {
  public static void main(String[] args)
  {
  StringBuffer s= new StringBuffer("good");
  StringBuffer s2=new StringBuffer("bad");
  test(s,s2);
  System.out.println(s); //9
  System.out.println(s2);//10
  }
  static void test(StringBuffer s,StringBuffer s2) {
  System.out.println(s);//1
  System.out.println(s2);// 2
  s2=s;//3
  s=new StringBuffer("new");//4
  System.out.println(s);//5
  System.out.println(s2);//6
  s.append("hah");//7
  s2.append("hah");//8
  }
  }
  The output of the program is:
  good
  bad
  new
  good
  goodhah
  bad
  exam Big tip: why is the output like this?
  What needs to be emphasized here is the "parameter passing mechanism", which is different from the passing mechanism in the assignment statement.
  We see that the output at 1,2 exactly matches what we expected.
  3 points s2 to s and 4 points s to a new object
  so the output of 5 prints the contents of the newly created object, while 6 prints the original The content of s
  7 and 8 modify the object content, but why is the output of 9 and 10 like that?
  The only parameter passing mechanism that Java applications have is pass-by-value.
  At this point, I would like to summarize my final thoughts on this issue and a method that I think can help you understand:
  we can understand objects in java as pointers
  in c/c++ For example in c/c++:
  int * p;
  print(p);//1
  *p=5;
  print(*p);//2
  What is the result printed by 1, a hexadecimal address, what is the result printed by 2? 5, which is the pointer pointed to.
  Even in c/c++, this pointer is actually a 32-bit integer, we can understand me a long value.
  What is an object s in java is also a pointer and an integer of int type (for JVM), we are using it directly (that is, in the case of s2=s, but for System.out.print(s ) This case is an exception, because it is actually an int integer when it is actually called by the ystem.out.print(s.toString())) object, which can explain both the assignment of the reference and the pass of the parameter. The value (in both cases is used directly), and when we are in the case of s.XXX, s is actually the use of *s in c/c++. This different result in different use cases is a simplification made by java for us, but may be misleading for c/c++ programmers. There are many cases in java where automatic recognition and processing according to the context are made, and the following is a somewhat extreme case:
  class t
  {
  public static String t="t";
  public static void main(String[] args)
  {
  tt = new t();
  tt();
  }
  static void t() {
  System.out.println(t);
  }
  }

1. The object is passed by reference
2. The original type is passed by value 3. Because the
String type does not provide its own modification function, each operation generates a new String object, so it needs special treatment. It can be considered as pass-by-value.

 

 

The fact that parameters are passed by value rather than by reference explains that the only mechanism for passing parameters that Java applications have is pass-by-value.

Objects are never passed in a Java application, only object references. Hence the object is passed by reference. The fact that Java applications pass objects by reference does not imply that Java applications pass parameters by reference. Parameters can be object references, and Java applications pass object references by value.

 

Variables in a Java application can be of one of two types: a reference type or a primitive type. Both types are handled the same way when passed as a parameter to a method. Both types are passed by value; none are passed by reference.

 

Pass-by-value and pass-by-reference. Pass-by-value means that when an argument is passed to a function, the function receives a copy of the original value. Therefore, if the function modifies the parameter, only the copy is changed, while the original value remains the same. Pass-by-reference means that when an argument is passed to a function, the function receives the memory address of the original value, not a copy of the value. Therefore, if the function modifies the parameter, the original value in the calling code also changes.

1. Objects are passed by reference
2. Java applications have one and only one parameter passing mechanism, which is pass-by-value
3. Pass-by-value means that when an argument is passed to a function, the function receives the original A copy of the value
4. Pass by reference means that when an argument is passed to a function, the function receives the memory address of the original value, not a copy of the value

First of all, let's take a look at the first point: objects are passed by reference.
  Indeed, I don't think you have any doubts about this, for example:
  class Test01
  {
  public static void main(String[] args)
  {
  StringBuffer s= new StringBuffer(" good");
  StringBuffer s2=s;
  s2.append(" afternoon.");
  System.out.println(s);
  }
  }
  Objects s ​​and s2 point to the same address in memory and therefore point to the same object .
  How to explain "objects are passed by reference"?
  The meaning here is that the object assignment operation is to pass the reference of the object, so the object is passed by reference, is there a problem?
  The output of the program is:
  good afternoon.
  This shows that s2 and s are the same object.
  One thing to clarify here is that passing an object here is actually passing a value, because an object is a pointer, and this assignment is an assignment between pointers, so it is called pass-by-reference in java. (What is a reference? Isn't it an address? An address is nothing but an integer value)
  Look at the following example again:
  class Test02
  {
  public static void main(String[] args)
  {
  int i=5;
  int i2=i;
  i2=6;
  System.out.println(i);
  }
  }
  What is the result of the program? 5! ! !
  What does this mean? Primitive data types are passed by value, and this pass by value also refers to the behavior of assignments. Next question: Java applications have only one parameter passing mechanism, which is pass by value
  class Test03
  {
  public static void main(String[] args)
  {
  StringBuffer s= new StringBuffer("good");
  StringBuffer s2=new StringBuffer("bad");
  test(s,s2);
  System.out.println(s); //9
  System.out.println(s2);//10
  }
  static void test(StringBuffer s,StringBuffer s2) {
  System.out.println(s);//1
  System.out.println(s2);// 2
  s2=s;//3
  s=new StringBuffer("new");//4
  System.out.println(s);//5
  System.out.println(s2);//6
  s.append("hah");//7
  s2.append("hah");//8
  }
  }
  The output of the program is:
  good
  bad
  new
  good
  goodhah
  bad
  exam Big tip: why is the output like this?
  What needs to be emphasized here is the "parameter passing mechanism", which is different from the passing mechanism in the assignment statement.
  We see that the output at 1,2 exactly matches what we expected.
  3 points s2 to s and 4 points s to a new object
  so the output of 5 prints the contents of the newly created object, while 6 prints the original The content of s
  7 and 8 modify the object content, but why is the output of 9 and 10 like that?
  The only parameter passing mechanism that Java applications have is pass-by-value.
  At this point, I would like to summarize my final thoughts on this issue and a method that I think can help you understand:
  we can understand objects in java as pointers
  in c/c++ For example in c/c++:
  int * p;
  print(p);//1
  *p=5;
  print(*p);//2
  What is the result printed by 1, a hexadecimal address, what is the result printed by 2? 5, which is the pointer pointed to.
  Even in c/c++, this pointer is actually a 32-bit integer, we can understand me a long value.
  What is an object s in java is also a pointer and an integer of int type (for JVM), we are using it directly (that is, in the case of s2=s, but for System.out.print(s ) This case is an exception, because it is actually an int integer when it is actually called by the ystem.out.print(s.toString())) object, which can explain both the assignment of the reference and the pass of the parameter. The value (in both cases is used directly), and when we are in the case of s.XXX, s is actually the use of *s in c/c++. This different result in different use cases is a simplification made by java for us, but may be misleading for c/c++ programmers. There are many cases in java where automatic recognition and processing according to the context are made, and the following is a somewhat extreme case:
  class t
  {
  public static String t="t";
  public static void main(String[] args)
  {
  tt = new t();
  tt();
  }
  static void t() {
  System.out.println(t);
  }
  }

1. The object is passed by reference
2. The original type is passed by value 3. Because the
String type does not provide its own modification function, each operation generates a new String object, so it needs special treatment. It can be considered as pass-by-value.

The fact that parameters are passed by value rather than by reference explains that the only mechanism for passing parameters that Java applications have is pass-by-value.

Objects are never passed in a Java application, only object references. Hence the object is passed by reference. The fact that Java applications pass objects by reference does not imply that Java applications pass parameters by reference. Parameters can be object references, and Java applications pass object references by value.

 

Variables in a Java application can be of one of two types: a reference type or a primitive type. Both types are handled the same way when passed as a parameter to a method. Both types are passed by value; none are passed by reference.

 

Pass-by-value and pass-by-reference. Pass-by-value means that when an argument is passed to a function, the function receives a copy of the original value. Therefore, if the function modifies the parameter, only the copy is changed, while the original value remains the same. Pass-by-reference means that when an argument is passed to a function, the function receives the memory address of the original value, not a copy of the value. Therefore, if the function modifies the parameter, the original value in the calling code also changes.

1. Objects are passed by reference
2. Java applications have one and only one parameter passing mechanism, which is pass-by-value
3. Pass-by-value means that when an argument is passed to a function, the function receives the original A copy of the value
4. Pass by reference means that when an argument is passed to a function, the function receives the memory address of the original value, not a copy of the value

First of all, let's take a look at the first point: objects are passed by reference.
  Indeed, I don't think you have any doubts about this, for example:
  class Test01
  {
  public static void main(String[] args)
  {
  StringBuffer s= new StringBuffer(" good");
  StringBuffer s2=s;
  s2.append(" afternoon.");
  System.out.println(s);
  }
  }
  Objects s ​​and s2 point to the same address in memory and therefore point to the same object .
  How to explain "objects are passed by reference"?
  The meaning here is that the object assignment operation is to pass the reference of the object, so the object is passed by reference, is there a problem?
  The output of the program is:
  good afternoon.
  This shows that s2 and s are the same object.
  One thing to clarify here is that passing an object here is actually passing a value, because an object is a pointer, and this assignment is an assignment between pointers, so it is called pass-by-reference in java. (What is a reference? Isn't it an address? An address is nothing but an integer value)
  Look at the following example again:
  class Test02
  {
  public static void main(String[] args)
  {
  int i=5;
  int i2=i;
  i2=6;
  System.out.println(i);
  }
  }
  What is the result of the program? 5! ! !
  What does this mean? Primitive data types are passed by value, and this pass by value also refers to the behavior of assignments. Next question: Java applications have only one parameter passing mechanism, which is pass by value
  class Test03
  {
  public static void main(String[] args)
  {
  StringBuffer s= new StringBuffer("good");
  StringBuffer s2=new StringBuffer("bad");
  test(s,s2);
  System.out.println(s); //9
  System.out.println(s2);//10
  }
  static void test(StringBuffer s,StringBuffer s2) {
  System.out.println(s);//1
  System.out.println(s2);// 2
  s2=s;//3
  s=new StringBuffer("new");//4
  System.out.println(s);//5
  System.out.println(s2);//6
  s.append("hah");//7
  s2.append("hah");//8
  }
  }
  The output of the program is:
  good
  bad
  new
  good
  goodhah
  bad
  exam Big tip: why is the output like this?
  What needs to be emphasized here is the "parameter passing mechanism", which is different from the passing mechanism in the assignment statement.
  We see that the output at 1,2 exactly matches what we expected.
  3 points s2 to s and 4 points s to a new object
  so the output of 5 prints the contents of the newly created object, while 6 prints the original The content of s
  7 and 8 modify the object content, but why is the output of 9 and 10 like that?
  The only parameter passing mechanism that Java applications have is pass-by-value.
  At this point, I would like to summarize my final thoughts on this issue and a method that I think can help you understand:
  we can understand objects in java as pointers
  in c/c++ For example in c/c++:
  int * p;
  print(p);//1
  *p=5;
  print(*p);//2
  What is the result printed by 1, a hexadecimal address, what is the result printed by 2? 5, which is the pointer pointed to.
  Even in c/c++, this pointer is actually a 32-bit integer, we can understand me a long value.
  What is an object s in java is also a pointer and an integer of int type (for JVM), we are using it directly (that is, in the case of s2=s, but for System.out.print(s ) This case is an exception, because it is actually an int integer when it is actually called by the ystem.out.print(s.toString())) object, which can explain both the assignment of the reference and the pass of the parameter. The value (in both cases is used directly), and when we are in the case of s.XXX, s is actually the use of *s in c/c++. This different result in different use cases is a simplification made by java for us, but may be misleading for c/c++ programmers. There are many cases in java where automatic recognition and processing according to the context are made, and the following is a somewhat extreme case:
  class t
  {
  public static String t="t";
  public static void main(String[] args)
  {
  tt = new t();
  tt();
  }
  static void t() {
  System.out.println(t);
  }
  }

1. The object is passed by reference
2. The original type is passed by value 3. Because the
String type does not provide its own modification function, each operation generates a new String object, so it needs special treatment. It can be considered as pass-by-value.

Guess you like

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