简体   繁体   中英

Java Passing array and object value

public static void main(String[] args) {
    Foo[] foo1 = new Foo[1];
    Foo[] foo2 = new Foo[1];
    foo1[0] = new Foo();
    foo2[0] = new Foo();
    foo1[0].x = 1;
    foo2[0].x = 2;
    swapA(foo1, foo2);
    System.out.println("Swap A " + foo1[0].x + " " + foo2[0].x);
    swapB(foo1[0], foo2[0]);
    System.out.println("Swap B " + foo1[0].x + " " + foo2[0].x);
    swapC(foo1[0], foo2[0]);
    System.out.println("Swap C " + foo1[0].x + " " + foo2[0].x);
    swapD(foo1, foo2);
    System.out.println("Swap D " + foo1[0].x + " " + foo2[0].x);
}

public static void swapA(Foo[] o1, Foo[] o2) {
    Foo[] temp;
    temp = o1;
    o1 = o2;
    o2 = temp;
    System.out.println("Swap A " + o1[0].x + " " + o2[0].x);
}

public static void swapD(Foo[] o1, Foo[] o2) {
    Foo temp;
    temp = o1[0];
    o1[0] = o2[0];
    o2[0] = temp;
    System.out.println("Swap D " + o1[0].x + " " + o2[0].x);
}

public static void swapB(Foo o1, Foo o2) {
    Foo temp;
    temp = o1;
    o1 = o2;
    o2 = temp;
    System.out.println("Swap B " + o1.x + " " + o2.x);
}

public static void swapC(Foo o1, Foo o2) {
    int temp;
    temp = o1.x;
    o1.x = o2.x;
    o2.x = temp;
    System.out.println("Swap C " + o1.x + " " + o2.x);
}


Swap A 2 1
Swap A 1 2
Swap B 2 1
Swap B 1 2
Swap C 2 1
Swap C 2 1
Swap D 1 2
Swap D 1 2

Just to want have a better understanding, why swapA and swapB doesn't change the value when print in the main method, but by itself method it change the value. I thought that object and array was pass by reference when you swap it the original value will change as well?

Please see Is Java "pass-by-reference" or "pass-by-value"?

The answer is no. Java is pass-references-by-value.

For example:

String a = "a string";
public void change(String b) {
    b = "some other string";
}
change(a);

The "pointer" to variable a is passed-by-value and assigned to b within the scope of the method. Changing b will alter b, but not a.

JAVA IS ALWAYS PASS BY VALUE!!!

Java references are passed by value.

Pass by reference/pass by value is an extremely annoying terminology.

hvgotcodes answer "references are passed by value" is the most obvious proof.

Java is consistent, just remember two things:

1) you can always re-assign the variables passed in to any method without effecting the caller.

2) Java doesn't duplicate objects, so if you have a given instance passed to you, modifying the data inside that instance will modify it for everyone.

By the way, the only case I've seen for modifying the values passed in is "swap", and that's kind of contrived since it's so simple that writing a generic version is pointless.

In general, modifying values passed in to a method is so rare and so confusing that Java just decided you don't need it. Not everyone thinks it's a good decision, but weather you agree that passing by reference is good or not you have to admit that not having it it allows you to forget a set of concerns (your variables will just never be modified when you call a method)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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