我正在用java編寫一些東西,最近我遇到了一些問題。簡而言之,我需要將我創建的對象與之前用不同數據實例化的這個非常類的另一個實例進行比較。在Java中傳遞對象引用的問題
看起來是這樣的:
a newA = null;
a oldA = null;
while(someBreakCondition) {
newA = new a();
newA.x = getSomeValue();
// now i want to compare the two objects
if(oldA != null) {
if(newA.time != oldA.time) {
// ...
}
}
// now my newA gets to be my oldA, since in the next iteration, a new newA is created
oldA = newA;
}
用類:
class a {
public long time;
public int x;
public a() {
time = System.currentTimeMillis;
}
}
的問題是,我最終找到了從紐瓦的值總是等於那些從oldA。所以我想......在循環的最後一行傳遞對象的引用出錯了...... 我認爲java總是傳遞對象的引用,除非顯式複製()被調用?
如果這確有影響:此代碼運行在Android上 - 不知道是否Dalvik虛擬機混亂與此aroung ...
由:http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html特別是nr。 9,命名約定 – wds 2010-06-25 07:59:07
「我最終發現newA的值總是等於那些來自oldA的值」 - 「I end up」的意思是「在'while循環」的末尾嗎?因爲在這種情況下,答案是微不足道的。 :s – 2010-06-25 08:38:30