1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class SimInt{ int value; public SimInt( int value){ this .value=value; } } public class
Main{ public static
void
change1(SimInt si){ si= new SimInt( 3 ); //重新指向了新的對象,原對象不受影響 } public static
void
change2(SimInt si){ si.value= 3 ; //通過引用操作對象內部成員,原對象被改變 } public static
void
main(String args[]) { SimInt
si1= new SimInt( 1 ); System.out.println(si1.value); //輸出1 change1(si1); System.out.println(si1.value); //輸出1 change2(si1); System.out.println(si1.value); //輸出3 } }
|
向相同的內存地址 所以doSomething(Integer integer)新建了引用integer 當doSomething(var2)
;被調用時 integer和var2指向了相同的內存地址. 當integer=newInteger(2);被調用時integer指向
了新的內存地址
但這並沒有改變var2指向的內存地址的值