在Java中传递值或通过引用传递:不可变和可变对象的问题

时间:2013-09-23 14:11:53

标签: java parameter-passing mutable immutability

java是按值传递还是通过引用传递。我的问题促使我写下这个class,以便我可以自信地回答。我想知道我注意到immutablemutable对象可能存在问题。在查看这个简单class的输出后,我问的是什么是正确的答案。

class

package notsure.tests;

public class PassingValues {

static Object[] passingValueMethod(int intValue, StringBuilder strValue){
    int recievedIntValue = intValue;
    StringBuilder recievedStrValue = strValue;

    System.out.println("------Let's see mutable objects------");
    System.out.println("----In the called method-------");
    System.out.println("-----New References Without Modification-----");
    //No modification
    System.out.println("Recieved integer: "+recievedIntValue);
    System.out.println("Received StringBuilder: "+ recievedStrValue);
    System.out.println();

    System.out.println("---- New refernces With Modification-----");
    //Modification
    recievedStrValue.append(", I am modified in a method() through a reference ");
    System.out.println("Recieved StringBuilder: "+ recievedStrValue);
    recievedIntValue++;
    System.out.println("Recieved integer: "+recievedIntValue);
    System.out.println();
    //Evaluate the parameter values
    System.out.println("----Received parameter variables current values-----");
    System.out.println("StringBuilder: "+strValue+" \nInteger: "+intValue);
    return new Object[]{recievedIntValue, recievedStrValue};

}
static String passingImmutable(String str){
    String recievedStr = str;
    System.out.println("-----In passpassingImmutable() ------");
    System.out.println("---------without modification------");
    System.out.println("Recieved string with local ref: "+recievedStr);
    System.out.println();
    System.out.println("------With modification-------");
    recievedStr = str+" I am modified";
    System.out.println("Recieved string with local ref: "+recievedStr);
    System.out.println();
    System.out.println("----Let's see the parameter value content---");
    System.out.println("Recieved string with param ref: "+str);
    return recievedStr;
}
public static void main(String[] args) {
    Object[] object = new Object[2];
    int integer = 10;
    StringBuilder stringBuilder=new StringBuilder("Stringbuilder");
    object = passingValueMethod(integer,stringBuilder);
    System.out.println();

    System.out.println("---------Back in Main-------- ");
    System.out.println("----Values returned----");
    for(Object obj:object){
        System.out.println(obj);
    }
    System.out.println();
    System.out.println("----Variables in Main-----");
    System.out.println(integer);
    System.out.println(stringBuilder);
    System.out.println("NOTE: even local Object(except primitive) reference reflect changes");
    System.out.println();
    System.out.println("-----Let's use immutable objects-----");
    String str = "I am a string";
    System.out.println("Value in main before method call: "+str);
    System.out.println();
    passingImmutable(str);
    System.out.println();
    System.out.println("--------------Back in main----------");
    System.out.println("String Value retuned: "+str);
    System.out.println();
    System.out.println("String passed(main reference) value: "+str);

}

}

output

------Let's see mutable objects------
----In the called method-------
-----New References Without Modification-----
Recieved integer: 10
Received StringBuilder: Stringbuilder

---- New refernces With Modification-----
Recieved StringBuilder: Stringbuilder, I am modified in a method() through a reference 
Recieved integer: 11

----Received parameter variables current values-----
StringBuilder: Stringbuilder, I am modified in a method() through a reference  
Integer: 10

---------Back in Main-------- 
----Values returned----
11
Stringbuilder, I am modified in a method() through a reference 

----Variables in Main-----
10
Stringbuilder, I am modified in a method() through a reference 
NOTE: even local Object(except primitive) reference reflect changes

-----Let's use immutable objects-----
Value in main before method call: I am a string

-----In passpassingImmutable() ------
---------without modification------
Recieved string with local ref: I am a string

------With modification-------
Recieved string with local ref: I am a string I am modified

----Let's see the parameter value content---
Recieved string with param ref: I am a string

--------------Back in main----------
String Value retuned: I am a string

String passed(main reference) value: I am a string

1 个答案:

答案 0 :(得分:1)

Java通过值传递引用,这使得它感觉是通过引用传递,尽管实际上是通过值传递。

public class Main{

    public static void main(String[] args) {
        Vector3d vectorTest=new Vector3d(1,2,3);
        System.out.println(vectorTest.x); //prints 1
        affectVector(vectorTest);
        System.out.println(vectorTest.x); //prints 100
        replaceVector(vectorTest);
        System.out.println(vectorTest.x); //still prints 100
    }

    public static void affectVector(Vector3d vectorIn){
         vectorIn.x=100; 
    }

    public static void replaceVector(Vector3d vectorIn){
         //this method has no external effect because the reference vectorIn is immediately overrwritten
         vectorIn=new Vector3d(0,0,0); //the reference vectorIn is completely changed
    }


}

您可以看到,因为引用按值传递,您仍然可以访问它引用的对象,但如果您替换引用,那么您将引用'到另一个对象,并且在方法之外没有任何影响。

我在尝试描述这个时使用的类比是邮政地址。引用是用于向对象发送字母(指令)的邮政地址。您可以将该地址复制到许多纸上,但它仍会将信件发送到同一所房子。这是复制的邮政地址,而不是' house'