Analysis of whether the content of the object changes after the java object is passed to a method as a method parameter

Question: When an object is passed as a parameter to a method, the method can change the properties of the object and return the changed result. So, is it passed by value or by reference?

       Answer: It is value transfer . In the Java language, only parameters are passed by value ; when an object instance is passed to a method as a parameter, the value of the parameter is a copy of the object reference. Pointing to the same object, the content of the object can be changed in the called method, but the object reference (not a copy of the reference) will never change .

       Java parameters: regardless of whether it is a primitive type or a reference type, what is passed is a copy (there is another way of saying that is passing by value, passing by value is generally relative to passing by address)

       If the parameter type is a basic data type , then what is passed is a copy of the parameter, that is , the value of the original parameter , which is the same as the value passing mentioned earlier. 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. This copy stores the address of the parameter . If the address of the copy is not changed in the function, but the value in the address is changed, then in the function Changes within will affect the incoming parameters. If the address of the copy is changed in the function, such as a new one, the copy will point to the new address. At this time, the passed-in parameter still points to the original address, so the original value will not be changed .

Basic data type parameter passing: does not change the value

 
  1. public static void test(int a){

  2. a=20;

  3. }

  4. public static void main(String[] args) {

  5. int a=10;

  6. test(a);

  7. System.out.println(a);

  8. }

The result is: 10

Variable object parameter passing: change the value

 
  1.   public static void test2(StringBuffer sb){

  2.         sb.append("add");

  3.     }

  4.     public static void main(String[] args) {

  5.         int a=10;

  6.         StringBuffer sb = new StringBuffer();

  7.         sb.append("init:");

  8.         test2(sb);

  9.         System.out.println(sb);

  10.     }

The result is: init: add

The encapsulated object is passed as a parameter, if the attribute value is modified in the method, the value will be changed

       Since the object is created before the method is passed, it is the same reference, so the content of the object will be changed

 
  1. public static void test3(A  bean){

  2.         bean.setName("李四");

  3.     }

  4.  
  5.     public static void main(String[] args) {

  6.         A  bean = new A();

  7.         bean.setName("张三");       

  8.         test3(bean);

  9.         System.out.println(bean.getName());

  10. }

  11.  
  12. class A {

  13.      private String name;

  14.  
  15.      public String getName() {

  16.          return name;

  17.      }

  18.  
  19.     public void setName(String name) {

  20.          this.name = name;

  21.      }

  22. }

The result is: Li Si

The encapsulated object is passed as a parameter, if the object is reassigned in the method, the value will be changed

       Since the object in the method is re-created, the reference is different, and the change of the object in the method will not affect the content of the outside object

 
  1. public static void test4(A bean){

  2. A bean2 = new A();

  3. bean2.setName("李四");

  4. bean = bean2;

  5. bean.setName("王犊子");

  6. }

  7.  
  8. public static void main(String[] args) {

  9. A bean = new A();

  10. bean.setName("张三");

  11. test4(bean);

  12. System.out.println(bean.getName());

  13. }

  14.  
  15. class A {

  16. private String name;

  17.  
  18. public String getName() {

  19. return name;

  20. }

  21.  
  22. public void setName(String name) {

  23. this.name = name;

  24. }

  25. }

The result is: Zhang San

Guess you like

Origin blog.csdn.net/xiaolegeaizy/article/details/108866205