Java changing the reference value data types

Java changing the reference value data types

In Java, the type of reference data is a copy of data transfer value (address)
for the following code

class BirthDate {
    private int day;
    private int month;
    private int year;
    
    public BirthDate(int d, int m, int y) {
        day = d; 
        month = m; 
        year = y;
    }
    
    public void setDay(int d) {
        day = d;
    }
    
    public void setMonth(int m) {
        month = m;
    }
    
    public void setYear(int y) {
        year = y;
    }
    
    public int getDay() {
        return day;
    }
    
    public int getMonth() {
        return month;
    }
    
    public int getYear() {
        return year;
    }
    
    public void display() {
        System.out.println
        (day + " - " + month + " - " + year);
    }
}

public class Test {
    public static void main(String args[]){
        Test test = new Test();
        int date = 9;
        BirthDate d1= new BirthDate(7,7,1970);
        BirthDate d2= new BirthDate(1,1,2000);    
        test.change1(date);
        test.change2(d1);
        test.change3(d2);
        System.out.println("date=" + date);
        d1.display();
        d2.display();
    }
    
    public void change1(int i){
        i = 1234;
    }
    
    public void change2(BirthDate b) {
        b = new BirthDate(22,2,2004);
    }
    
    public void change3(BirthDate b) {
        b.setDay(22);
    }
}

Above is a peer birthday initialize the object, and modify the program. Program initialized by the constructor, and try to use different methods to modify the stored value of the object.

Birthdate reference is a data type, change the value can not simply pass the value stored in the object should be used in a particular method setDay Birthdate class method.

It illustrates the value of reference data transfer and memory type change

After the stack memory, the values ​​stored in the heap memory objects, it initializes local variable storage, distribution of memory will be presented below in FIG.

PotPlayerMini64_Lj4JOGFnoF.png

At this time, the value stored in the stack memory objects Test, d1, d2, address and date of local variables, the heap memory stores values ​​d1 and d2, and the stack memory by the address point d1 and d2 each heap memory value.

And after the execution of the program when the following paragraph

        test.change1(date);
        /*
          省略中间的代码
        */

    public void change1(int i){
        /*
          尚未执行的代码
        */
    }

At this time, as shown in FIG memory

PotPlayerMini64_cI7fsNJcxJ.png

Change1 In the method, a new block is allocated in the stack memory space in the form of a parameter i, and pass it to a date value by assigning a value, which is 9.

When starting the execution of code segment

    public void change1(int i){
        i = 1234;
    }

PotPlayerMini64_BOCqo5i6lq.png

The value of the parameter i is changed in the form of 1234, the value date is not changed.

When change1 method is finished, the space for local variables assigned all but disappeared, and the value date is still 9, modification failed.

PotPlayerMini64_Lj4JOGFnoF.png

Continue change2

        test.change2(d1);
        /*
          省略中间的代码
        */

    public void change2(BirthDate b) {
        /*
          尚未执行的代码
        */
    }

At this time, as shown in FIG memory

PotPlayerMini64_x5dlZKd9EM.png

The system opens a new stack memory space for b, and the target value d2 (address) to the formal parameters of the object b, b so that the address points to an address and d2 refer to the same memory space heap, i.e. pointing object value d2 is stored. Seems necessary to amend successful, is not it?

However, the implementation code segment below

    public void change2(BirthDate b) {
        b = new BirthDate(22,2,2004);
    }

GmUsTgE52X.png

At this point b to point to the new address out of the new object, content quietly changed.

When change2 method is finished, the space for local variables assigned all but disappeared, the value stored in the heap memory garbage collection will recover after a period of time, the value of d2 has not changed, modified still fail.

PotPlayerMini64_Lj4JOGFnoF.png

Finally, look at the code segment change3

        test.change3(d2);
        /*
          省略中间的代码
        */

    public void change3(BirthDate b) {
        /*
          尚未执行的代码
        */
    }

PotPlayerMini64_x5dlZKd9EM.png

Similar change2, system parameters b to form allocated space, the object points to a value d2

The next execution method setDay

    public void setDay(int d) {
        day = d;
    }
        /*
          省略中间的代码
        */
    public void change3(BirthDate b) {
        b.setDay(22);
    }

setDay class Birthdate method of, after calling setDay method will setDay formal parameters (int d) d is transferred to the day the member variable, b is reflected in the attribute memory 22 becomes day, since b and d2 in heap memory is pointed to the same memory space, so the modification is successful.

PotPlayerMini64_9uwcBvSpMm.png

Finally b this memory disappear, then the value of d2 Birthdate objects has changed.

PotPlayerMini64_OB4T6bSMh1.png

Guess you like

Origin www.cnblogs.com/HuoHua2020/p/12331890.html