简体   繁体   English

将对象传递给方法java

[英]Passing objects to methods java

I'm using three objects: StringBuilder , Integer , and testobject - that are passed to a method to change its state. 我正在使用三个对象: StringBuilderIntegertestobject - 它们被传递给一个方法来改变它的状态。

As expected, the StringBuilder and testobject points to the same object and the state is change but it does not work for Integer object. 正如所料, StringBuildertestobject指向同一个对象,状态发生了变化,但它不适用于Integer对象。

class testobject{
    int x = 1;
}

public class test{
    public static void main(String[] args){
        StringBuilder s1 = new StringBuilder("String");
        go(s1);
        System.out.println(s1);

        Integer s2 = new Integer("20");
        go1(s2);
        System.out.println(s2);

        testobject s3 = new testobject();
        go2(s3);
        System.out.println(s3.x);
    }

    static void go(StringBuilder s1){
        s1.append("Builder");
    }

    static void go1(Integer s2){
        s2 = 1;
    }
    static void go2(testobject s3){
        s3.x = 5;
    }

Result: 结果:

StringBuilder
20
5

ExpectedResult: 预期结果:

StringBuilder
1
5

Look at your three methods: 看看你的三种方法:

static void go(StringBuilder s1){
    s1.append("Builder");
}

static void go1(Integer s2){
    s2 = 1;
}

static void go2(testobject s3){
    s3.x = 5;
}

In go and go2 , you're making a modification to the object that the parameter value refers to. gogo2 ,您正在修改参数值所引用的对象。

In go1 , you're changing the value of the parameter variable itself. go1 ,您正在更改参数变量本身的值。 That's very different, and because Java always uses pass-by-value , that change isn't seen by the caller. 这是非常不同的,因为Java总是使用pass-by-value ,调用者看不到这种变化。

It's important to understand that objects aren't passed to the methods at all. 重要的是要理解对象根本不会传递给方法。 Instead, references are. 相反,引用是。 The value of s1 , s2 and s3 are all references. s1s2s3的值都是引用。 If you think of the variables as like pieces of paper, each piece of paper has a house address on it, which was copied from a piece of paper declared in main . 如果你觉得这些变量的像张纸,每张纸上有一所房子的地址,这是在宣布一张纸上复印main

The method bodies of go and go2 are like visiting the house whose address is on the piece of paper, and painting the front door. gogo2的方法体就像访问地址在纸上的房子,并在前门上画画。 If you then visit the houses using the original pieces of paper, you still see the new colours on the front doors. 如果您使用原始纸张访问房屋,您仍然可以看到前门上的新颜色。

The method body of go1 is like scribbling out the address written on the piece of paper, and writing a new one on there instead. go1的方法体就像go1在纸上的地址,然后在那里写一个新地址。 That doesn't make any change to a house, nor does it change the original piece of paper. 这不会改变房子,也不会改变原始的纸张。

If the argument you want to pass is an object instead a of primitive type, then Java treats pass by object as pass by value, because Java supports a pass-by-value concept. 如果要传递的参数是对象而不是基本类型,则Java将按对象传递视为按值传递,因为Java支持按值传递概念。

Here a and p are the same reference: 这里a和p是相同的参考:

public class ObjPass {
    int value;
    public static void main(String[] args) {
        ObjPass p = new ObjPass();
        p.value = 5;
        System.out.println("Before calling: " + p.value);
        increment(p);
        System.out.println("After calling: " + p.value);       
    }
    public static void increment(ObjPass a){
        a.value++;

    }

}

Output: 输出:

Before calling: 5
After calling: 6        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM