Java中參數(shù)傳遞是傳值還是傳引用呢?很多人遇到這個(gè)問題都會(huì)馬上給你拋出這個(gè)例子:
class Entry{ Integer value; public Entry(Integer v){ this.value = v; } @Override public String toString() { return "Entry[value=" + value + "]"; } }public class CallByDemo{ public static void swap(int a,int b){ int temp = a; a = b; b = a; } public static void swap(Entry e1,Entry e2){ Integer temp = e1.value; e1.value = e2.value; e2.value = temp; } public static void main(String[] args) { int a = 1; int b = 2; System.out.println("before:a="+a+",b="+b); swap(a,b); System.out.println("after :a="+a+",b="+b); Entry e1 = new Entry(new Integer(1000)); Entry e2 = new Entry(new Integer(2000)); System.out.println("before:e1="+e1+"e2="+e2); swap(e1,e2); System.out.println("after :e1