java是按值传递还是按引用传递。我的问题促使我写下这篇class
文章,以便我可以自信地回答。immutable
正如我想知道的那样,我注意到和mutable
对象可能存在问题。我要问的是在查看这个简单的输出后正确的答案是什么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