I realize that if I pass an object as a parameter of a function and do changes to it, the changes “stay” with the object. But it is not the case for an integer.
public void start() {
int x = 100;
modify(x);
// I would like x to be 200 now. But it isn't :(
}
public void modify(int y) {
y *= 2;
}
So basically, is there a way to achieve what I wanted in the code above? Is it possible to modify an integer like that (like an object reference)?
While working with
primitivesthere is no concept of “reference“. But you may achieve what you want by doing something like below:x = modify(x);may be code want.Now
xcontains the results ofmodify(x)method invocation.