刚看了一遍java in a nutshell..明白了一点。ia=null是一个空引用。也就是不对任何对象进行引用,所以也就不会对原始值产生影响。。:)
Point q = new Point(3.0, 4.5); // A point with an X coordinate of 3 changeReference(q); // Prints 3,2,1 and modifies the Point System.out.println(q.x); // The X coordinate of q is now 0! When the changeReference() method is invoked, it is passed a copy of the reference held in variable q. Now both the variable q and the method parameter p hold references to the same object. The method can use its reference to change the contents of the object. Note, however, that it cannot change the contents of the variable q. In other words, the method can change the Point object beyond recognition, but it cannot change the fact that the variable q refers to that object.
ia.append(" ok?");改成ia=null;试试。看看两次的结果 有什么不同?
当然会改变如果要传值,使用Class.clone()
看来,传值与传址你还有点不清
ia 是一个引用(a)的副本
他与a指向同一个对象
故若你用ia.append("ok?") 是对对象的操作
但你用ia=null,是对这个引用值的操作。不会影响原对象。
根据上述现象,我理解的是ia.append()是传址,ia=null是传值,对吗?这样一来我又迷糊了。。:(
changeReference(q); // Prints 3,2,1 and modifies the Point
System.out.println(q.x); // The X coordinate of q is now 0!
When the changeReference() method is invoked, it is passed a copy of the reference held in variable q. Now both the variable q and the method parameter p hold references to the same object. The method can use its reference to change the contents of the object. Note, however, that it cannot change the contents of the variable q. In other words, the method can change the Point object beyond recognition, but it cannot change the fact that the variable q refers to that object.