java 引用传递、返回 疑惑
高洛峰
高洛峰 2017-04-17 13:36:12
0
8
417

代码如下:

public static void main(String[] args) {
        DispatcherServlet dispatcherServlet = new DispatcherServlet();
        A a = dispatcherServlet.new A("bb");
        dispatcherServlet.da(a); // 此行执行完毕,a.getA()值是bb
        System.out.println(a.getA());
    }

    private A da(A a) {
        a = new A("aaaaa");
        return a;
    }

    class A {
        private String a;

        public A(String a) {
            this.a = a;
        }

        public String getA() {
            return a;
        }

        public void setA(String a) {
            this.a = a;
        }

    }

疑惑如注释。为何da方法里的new值回到main后会丢失?

经学习得出以下结论:

  1. java只有值传递,没有引用传递
  2. 非原始类型也是值传递,传递的是值是该引用,为副本

因此:

  1. 在da中修改a的属性值,可以在da方法外生效。
  2. 在da中修改a的引用(重新对a赋值),修改的是引用副本。在da方法外不生效!

希望对其它碰到这种问题的人,不明所以的人有所帮助!!哈哈\(^ ^)/

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(8)
巴扎黑

Although Java does not write pointers in plain text, pointers are still used.
dispatcherServlet.da(a); // Although the object a is written here, the reference

is used in the method
private A da(A a) { // 这里是一个指针对象, 指向上方传入的 那个对象
    a = new A("aaaaa"); // 这里只是改变了 指针对象 并没有修改 源对象.
    return a; // 如果你想获得新的对象, 那就取这个返回值来用吧.
}

Java is divided into objects, and reference objects (or pointers?) to this object.
Example:
Goods a in the warehouse, and the list of goods a qd_a, the list states which warehouse, which area and location in which goods a is located.
Someone changed the data on qd_a and pointed it to item b, but item a was not affected. What was affected was the list of qd_a.

This is what I understand. I don’t know if the explanation is clear. I hope you can correct me.

迷茫
    private A da(A a) {
        a = new A("aaaaa");
        return a;
    }

Change a here to a member variable

阿神
Cprivate:
    A da(A &a) {
        a = new A("aaaaa");
        return a;
    }

Java does not pass by reference, which means that Java cannot let you replace the reference of the entire parameter. C/C++ has reference passing, but you must add the ampersand in front of the parameter.

Peter_Zhu

Java passes by value, not by reference, but by passing the value of the pointer, so it looks like a reference. . .

Inside

a = new A("aaaaa");

This sentence cannot be reflected outside the function. If the value (address) of a is modified within the function, it is useless, but the content of a can be modified
For example, if there is

in da
a.setA("aaaaa");

Then the value you print outside the function is "aaaaa"

Equivalent to C++ like this

void f(A *a) {
    a = new A("aaaaa");
}

void main() {
    a = new A("bb");
    f(a);
    cout << a.getA(); // "bb"
}
左手右手慢动作

Java only passes by value, not by reference
Correct answer, when passing an object, you must remember that what you pass is not the memory address of the object itself, but the value of the object, and this value is the address referenced by the object

刘奇

I feel like Java passes parameters on a case-by-case basis,
For container objects such as Map, the reference is passed instead of copying the value
The processing of the map container in the function will change to the original caller. This cannot be called passing by value...
It’s true that simple types are passed by value

伊谢尔伦

Posts in 2009
http://ivan-pig.iteye.com/blog/422891
Can this solve lz’s question?

黄舟

For non-primitive types, the reference is placed on the stack and the object itself is placed on the heap. The reference on the stack will be copied, but the object will not be copied.

If you want to implement reference transfer in Java, you can use boxing, that is, use an array to access.

private void foo(int[] iarr)
{
    int ival = iarr[0];
    // ...
    iarr[0] = ival;
}
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!