关于Java引用传递的一个困惑?
ringa_lee
ringa_lee 2017-04-18 10:47:30
0
6
512
    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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!