本隨筆旨在強化理解傳值與傳引用 如下代碼的運行結果 其中i沒有改變,s也沒有改變。 但model中的值均改變了。 i :100s :hellomodel :testchangemodel2 :changeModel i :100s :hellomodel :testchangemodel2 :cha ...
本隨筆旨在強化理解傳值與傳引用
如下代碼的運行結果 其中i沒有改變,s也沒有改變。 但model中的值均改變了。i :100
s :hello
model :testchange
model2 :changeModel
package newtest; public class testman { class Model { int i = 0; public String s = "no value"; } public static void changeInt(int i) { i = 10; } public static void changeString(String s) { s = "test"; } public static void changeModel(Model model) { model.i = 10; model.s = "testchange"; } public static void changeModel2(Model model) { model.i = 10; model.s = "changeModel"; } public static void main(String[] args) { int i = 100; String s = "hello"; Model model = new testman().new Model(); Model model2 = new testman().new Model(); changeInt(i); System.out.println("i :" + i); changeString(s); System.out.println("s :" + s); changeModel(model); System.out.println("model :" + model.s); changeModel2(model2); System.out.println("model2 :" + model2.s); } }