public static Object change(Object obj) {
System.out.println(" in change method, the obj hash code is:" + obj.hashCode());
if (obj instanceof Map) {
((Map) obj).put("key", "1");
//System.out.println(" in change method, after set value the 'Map' hash code is:" + obj.hashCode());
} else if (obj instanceof Number) {
obj = 2;
//System.out.println(" in change method, after set value the 'Number' hash code is:" + obj.hashCode());
} else if (obj instanceof String) {
obj = 3;
//System.out.println(" in change method, after set value the 'String' hash code is:" + obj.hashCode());
}
return obj;
}
public static Long change(Long b,Long a) {
b = 1L;
a = b;
System.out.println("內部測試結果:"+a);
return b;
}
public static int change(int a) {
a = 1;
System.out.println("內部測試結果:"+a);
return a;
}
public static void main(String[] args) {
Map map = new HashMap();
System.out.println("out change method, the old 'map' hash code is:" + map.hashCode());
map.put("key", "0");
change(map);
System.out.println(map.get("key"));
Long l = new Long(0);
System.out.println("out change method, the old 'l' hash code is:" + l.hashCode());
change(l);
System.out.println(l);
String s = new String("0");
System.out.println("out change method, the old 's' hash code is:" + s.hashCode());
change(s);
System.out.println(s);
String as = new String("abc");
String bs = "abc";
String cs = "abc";
String ds = new String("abc");
System.out.println(as==bs);
System.out.println(bs==cs);
System.out.println(cs==ds);
System.out.println(ds==as);
Long la = 0L;
Long la1 = 2L;
change(la1,la);
System.out.println("Long la1:"+la);
Long la2 = new Long(2);
Long change2 = change(la2,la);
System.out.println("Long la2:"+la);
int ia = 0;
change(ia);
System.out.println("int ia:"+ia);
}
輸出結果
out change method, the old 'map' hash code is:0
in change method, the obj hash code is:106095
1
out change method, the old 'l' hash code is:0
in change method, the obj hash code is:0
0
out change method, the old 's' hash code is:48
in change method, the obj hash code is:48
0
false
true
false
false
內部測試結果:1
Long la1:0
內部測試結果:1
Long la2:0
內部測試結果:1
int ia:0
解答方案:堆棧的存儲問題