亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

關于Java引用傳遞的一個困惑?
ringa_lee
ringa_lee 2017-04-18 10:47:30
0
6
609
    public static void swapEqual(int[] a,int[] b){
        int[] temp=new int[b.length];
        temp=b;
        b=a;
        a=temp;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] c={1,2};
        int[] d={3,4};
        swapEqual(c,d);
        System.out.println(c[1]);
    }

為什么打印出來的C[1]還是原來的2啊,為什么沒有交換。數組的=不就是復制引用嗎?通過函數可以把a,b的引用交換,這樣不就是把內容交換了嗎?
干嘛非要這樣寫?
public static void swap(int[] a,int[] b){
    int[] temp=new int[b.length];
    for(int i=0;i<b.length;i++){
        temp[i]=b[i];
    }
    for(int i=0;i<b.length;i++){
        b[i]=a[i];
    }
    for(int i=0;i<b.length;i++){
        a[i]=temp[i];
    }
}

這樣子我試了一下,就可以達到交換的目的了
好困惑啊,求解??!

ringa_lee
ringa_lee

ringa_lee

reply all(6)
PHPzhong

In

swapEqual(int[] a,int[] b):
At the beginning:
a --> {1,2} (address 1)
b --> {3,4} (address 2)
After processing by your code (the addresses pointed to by a and b are exchanged):
b --> {1,2} (address 1)
a --> {3,4} (address 2)
But it won't Affects c, d, c, d are still:
c --> {1,2} (address 1)
d --> {3,4} (address 2)

//==================================

In

swap(int[] a,int[] b):
At the beginning:
a --> {1,2} (address 1)
b --> {3,4} (address 2)
After processing by your code (the values ??in addresses 1 and 2 are exchanged):
a --> {3,4} (address 1)
b --> {1,2} (address 2)
Although not It will affect the addresses pointed to by c and d, but the value under the original address has changed:
c --> {3,4} (address 1)
d --> {1,2} (address 2 )

//========================================

My understanding is this. After writing it, I found that it has the same meaning as @lianera’s expression, so I treated it as a supplementary explanation.

//========================================

You can try to use return to assign the swapped values ??of a and b in swapEqual(int[] a, int[] b) to c and d and see the result.

Ty80

In the swapEqual function, only the references of the formal parameters a and b are changed, but the references of the actual parameters c and d are not changed.
In swap, the values ??of the areas corresponding to c and d are changed.
To add, arrays in Java are also objects, so a, b, c, and d are all references.

伊謝爾倫

The concept of references in Java is a little different from that in C++. References in Java are equivalent to pointers in C++, so you cannot change the actual parameters by directly assigning values ??to the formal parameters.

Peter_Zhu

Look at this answer. https://www.zhihu.com/questio...

阿神

Java references (including basic types, object reference types) will generate new references during declarations, method calls, etc., copy the reference on the right side of the equal sign. Divided into the following 3 situations:

  1. The value represented by the basic type is stored in the reference. There is a special area in the reference to store this value, so when copying, the value is also copied at the same time.

  2. This area of ??reference type stores the memory address of the object in the heap memory. When the reference is copied, the memory address pointed to is the same, so the copy of the value (that is, the object) will not be involved

  3. Arrays contain stored references (including basic types and object reference types)


Reference information: In Java, do reference data types occupy memory?

To understand this problem, you must first understand that there are four categories and eight basic types in JAVA. Except for the basic types, they are all reference types. For example, if you write int i = 1;, then its allocation in the memory is like this: a space is allocated in the memory, the name of this space is i, and the content inside is 1.

When you use i, you can access the content in this space. The reference type is different. The reference type occupies two blocks of memory in the memory. For example: you write String s; or String s = null; at this time, a piece of memory is allocated in the memory. This memory is loaded with the null value, which means nothing is loaded. Because it has not been initialized yet. Previous picture:

As for where this s is allocated, it depends on where it is declared. If s is declared as a local variable, then s is in the stack space. If it is not a local variable, it is not allocated on the stack. But when you use s to point to an object of type String, something changes. That is, when you next write s = new String("zhihu");. Previous picture:

There will be a value in the original s. Based on this value in the space of s, you can find another piece of memory on the heap. All new things are in the heap memory. String attributes are allocated in this memory on the heap. Heap memory is dynamically allocated memory. So since it is allocated on the heap, it actually means that it is not certain how much memory the new object occupies. It can only be allocated during runtime to understand how big the object is allocated. Another reason why the memory occupied cannot be determined is that the method only allocates memory when it is executed. If no method is called, the method is just a bunch of code and does not occupy memory.

伊謝爾倫

Thank you everyone for your answers, they are all very good. I understand too

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template