String old = "111"; old = "222"; is it means old is changed to new value?
crazyjava网友说得对。 String old = "111"; old = "222"; 这时old对象的地址已经不是原来的了, 即old对象已经不是原来的old对象了。 相当于 String old = new String("111"); old = new String("222"); String对象的方法中如果涉及到值发生变化的话,都是返回一个新的String对象。 Integer的值不能改变,所以只能新建一个对象了,例如 Iterger nCount = new Integer(1); int n = nCount.intValue(); n ++; nCount = new Integer( n ); //将nCount的值转为int,对int值进行操作,然后再转化回Integer
old = "222";
is it means old is changed to new value?
String old = "111";
old = "222";
这时old对象的地址已经不是原来的了,
即old对象已经不是原来的old对象了。
相当于
String old = new String("111");
old = new String("222");
String对象的方法中如果涉及到值发生变化的话,都是返回一个新的String对象。
Integer的值不能改变,所以只能新建一个对象了,例如
Iterger nCount = new Integer(1);
int n = nCount.intValue();
n ++;
nCount = new Integer( n );
//将nCount的值转为int,对int值进行操作,然后再转化回Integer