java-based: the value of the method call is passed call by value, and transmits the copy of the value of the parameter, rather than a reference

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));
}

  

Export

460141958
1163157884
1163157884

  

java-core  P121 

The method of passing parameters to the java, always call by value call by value.

In the method, if the parameter is the object that the operation is a copy of the referenced object.

If the parameter is a value, the value is a copy operation .

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

  

 

Guess you like

Origin www.cnblogs.com/zhizhiyin/p/11099253.html