public class TestExtends {
public static void main(String[]args){
int s = 10;
System.out.println(System.identityHashCode(s));
s=3*s;
int num = 30;
System.out.println(System.identityHashCode(s));
System.out.println(System.identityHashCode(num));
}
输出
460141958
1163157884
1163157884
java-core P121
java中参数传递给方法,总是按值调用call by value。
在方法中,如果参数是对象,那操作的是对象的引用的拷贝。
如果参数是值,操作的是值的拷贝。
public class TestExtends {
public static void main(String[]args){
A a =new A("bob");
A b = new A("lucy");
a.swap(a,b);
System.out.println(a.getName()+" "+b.getName());
A temp = new A("");
temp=a;
a=b;
b=temp;
System.out.println(a.getName()+" "+b.getName());
int i =1;
int j = 2;
a.swap2(i,j);
System.out.println(i+" "+j);
}
}
class A{
public int x=1;
void swap(A a,A b){
A temp = new A("---");
temp=a;
a=b;
b=temp;
}
void swap2(int a ,int b){
int c = 0;
c=a;
a=b;
b=c;
}
private String name;
public A(String name){
this.name = name;
}
public String getName(){
return name;
}
}
bob lucy lucy bob 1 2