Value passing and reference passing

First of all, don't dwell on the literal meaning of Pass By Value and Pass By Reference, otherwise it's easy to fall into the so-called meaningless debate that "all pass by references are essentially Pass By Value".
What's more, if you want to know whether Java is passing values or references, at least you need to know the exact meaning of passing values and references first? However, if you already know the exact meaning of these two names, you can judge whether Java passes values or references.
This is like using the term of university to explain the topic of high school, which has no meaning for beginners.

1: Figure out the differences between basic types and reference types

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

As shown in the figure, num is the basic type, and the value is saved directly in the variable. str is a reference type, and only the address of the actual object is saved in the variable. This variable is generally called "reference". The reference points to the actual object, which holds the content.

2: Find out what the assignment operator (=) does

num = 20;
str = "java";

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


3: What happens when a method is called? Parameter passing is basically an assignment operation.
First example: basic type

void foo(int value) {
    value = 100;
}
foo(num); // num has not been changed

The second example: does not provide a reference type to change its own method

void foo(String text) {
    text = "windows";
}
foo(str); // str has not been changed

Third example: provides a reference type that changes its own method

StringBuilder sb = new StringBuilder("iphone");
void foo(StringBuilder builder) {
    builder.append("4");
}
foo(sb); // sb was changed to "iPhone 4".

The fourth example: provides a reference type to change its own method, but does not use, but uses the assignment operator.

StringBuilder sb = new StringBuilder("iphone");
void foo(StringBuilder builder) {
    builder = new StringBuilder("ipad");
}
foo(sb); // sb hasn't been changed, it's still "iphone".

Focus on understanding why the results of the third example and the fourth example are different?

The following is an illustration of the third example:


builder. After append ("4")


The following is an illustration of the fourth example:


builder = new StringBuilder("ipad"); after

Author: Intopass
Link: https://www.zhihu.com/question/31203609/answer/50992895
Source: Zhihu
The copyright belongs to the author. For reprint, please contact the author for authorization.

Interview questions:

given the following code,what will be the output?  A

class Value{

    public int i=15;

}

public class Test{

    public static void main(String argv[]){

        Test t=new Test( );

        t.first( );

    }





   public void first( ){

      int i=5;

      Value v=new Value( );

      v.i=25;

      second(v,i);

      System.out.println(v.i);

   }





   public void second(Value v,int i){

     i = 0;

     v.i = 20;

     Value val = new Value( );

     v = val;

     System.out.println(v.i+" "+i);

   }

}

A,15 0 20

B,15 0 15

C,20 0 20

D,0 15 20

Give the following code, please give the result B

class Two{

    Byte x;

}

class PassO{

    public static void main(String[] args){

        PassO p=new PassO();

        p.start();

    }

    void start(){

        Two t=new Two();

        System.out.print(t.x+"");

        Two t2=fix(t);

        System.out.print(t.x+" " +t2.x);

    }

    Two fix(Two tt){

        tt.x=42;

        return tt;

    }

}



A,null null 42

B,null 42 42

C,0 0 42

D,0 42 42

E,An exception is thrown at runtime

F,Compilation

Byte here is the wrapper type of byte, which is initialized to null instead of 0

24. Indicate the results of the following procedures ()

public class Example {



    String str = new String("good");



    char[] ch = { 'a', 'b', 'c' };



    public static void main(String args[]) {



        Example ex = new Example();

// ex.str is called an argument

        ex.change(ex.str, ex.ch);



        System.out.print(ex.str + " and ");



        System.out.print(ex.ch);



    }



     // str is called formal parameter

    public void change(String str1, char[] ch1) {



        str1 = "test ok";



        ch1[0] = 'g';



    }

}

A, good and abc

B, good and gbc

C, test ok and abc

D, test ok and gbc 

Answer: B

Parsing: you may think that strings and arrays in Java are objects, so they must be object references, and then you will choose D. in fact, this is a big misunderstanding: there is no reference transfer in Java, only value transfer, and reference data types transfer address values,

Assign the address value of the argument to the formal parameter. The argument and the formal parameter point to the same object through the address value. You can modify the content of the address through it (the reference remains unchanged), because the address of the content is the same as the original address,

Keywords: Java

Added by jtravis on Thu, 23 Dec 2021 01:00:49 +0200