In the following code, the effect of exchanging two objects is not achieved
The output result is 3:4
Logically speaking, aren't all references in Java? You should be able to exchange objects directly!
Please explain my error and give a solution.
I hope the output result is 4:3
class Int
{
public int x;
}
public class Hello {
void swap(Int a,Int b)
{
Int t=a;
a=b;
b=t;
}
public static void main(String[] args) {
Hello hello=new Hello();
Int a=new Int();
Int b=new Int();
a.x=3;
b.x=4;
hello.swap(a,b);
System.out.println(a.x+":"+b.x);
}
}
In the Java world, the input parameters of functions or methods are passed through value copy:
Normally, java cannot implement it.
And the inside of the int object
private final int value;
is final