Apologies for asking such a primitive question.
I wrote a function which took one parameter; this parameter was supposed to be an in/out parameter.
After debugging, I realized, since there are no explicit reference types in java, I should probably use Integer object (instead of int primitive) as the parameter type. So I changed my function:
boolean getNextFoo(int currentFoo) {
currentFoo = currentFoo + 1;
}
to:
boolean getNextFoo(Integer currentFoo) {
currentFoo = currentFoo + 1;
}
After checking the execution in the debugger, I realized this has the same results as the previous. That is, the object that I'm passing into getNextFoo()
does not change.
Question 1: How to I achieve in/out parameters with Java (more specifically for primitive types). I thought (pardon me) that objects are always passed by reference.
Question 2: I was checking on the internet for answers and read that Integer is immutable. I understand immutable means constant. If so, why doesn't the compiler (interpreter?) complain when I do currentFoo = currentFoo + 1
within the getNextFoo
function?
PS: I see in the debugger that Integer object is actually not being passed to getNextFoo
, instead the value of Integer.valueOf
is being passed into getNextFoo
which is leaving me even more confused.