In Java int is a primitive type. So for any primitive type you can't do it. There is a boxed primitive for every primitive in Java, but here Integer is immutable (once initialized can't change it's state) so even using Integer won't work.
So you have two solutions:
1) Use MutableInt from apache commons jar.
2) Create a wrapper class for it as below and use that.
public class MutableInt {
private int myInt; //default initialized with 0
public int getMyInt(){
return myInt;
}
public void setMyInt(int myInt) {
this.myInt = myInt;
}
}
And inside the change
method
public static void change(MutableInt x, MutableInt y){
x.setMyInt(x.getMyInt() + y.getMyInt());
y.setMyInt(x.getMyInt() - y.getMyInt());
x.setMyInt(x.getMyInt() - y.getMyInt());
}
Then when you get the value from x or y using x.getMyInt() or y.getMyInt()
, you will get updated value.