【发布时间】:2017-06-23 12:17:48
【问题描述】:
考虑下面的代码
private static boolean noHashClash(JSONArray ja,String hash,long epoch,long
stay)
{
int i,diff,len = ja.length();
String oHash;
JSONObject pjo;
try
{
for(i=0;i < len;i++)
{
pjo = ja.optJSONObject(i);
oHash = pjo.optString("hash","");
if ((null == pjo) || (0 == oHash.length())) continue;
diff = TLSH.totalDiff(hash,oHash,false);
if (Geo.hashBlur > diff)
{
pjo.accumulate("ats",epoch);
pjo.accumulate("stays",stay);
int times = pjo.optInt("times",0);
pjo.put("times",times + 1);
return false;
}
}
return true;
} catch(Exception e)
{
Feedback.recordError(Utils.errorString(e));
return true;
}
}
我在这里所做的是将哈希值与 JSONArray 中对象的哈希值进行比较。如果找到哈希“匹配”(模糊),则函数返回false。在这样做之前,它会修改匹配的对象 - 行
pjo.accumulate("ats",epoch);
....
pjo.put("times",times + 1);
Whist 编译并正确运行,当我保存并检索该 JSONArray 时,我发现更改没有卡住。我的理解是Java按值传递函数参数,其中对象参数的“值”是实际对象本身。
这意味着我对函数内的对象属性所做的任何更改都应该保留,并且确实发生了。那么为什么嵌套对象在这里显然受到不同的对待。我怀疑我对这些事情在 Java 中如何工作的理解存在差距。
【问题讨论】:
标签: java reference parameter-passing