Anitha Ramaiah wrote:
We have a requirement in which a date object is incremented until it is not a holiday. I have provided the code below.
When I debugged the program, the variable crValDt shows the correct value at each places. But after returning, when I printed the value, it is just +1 to the passed date.
My debugging and analysis:
I tried the same with String instead of date. It behaves the same. The highlighted method call gets stacked up and hence when popped off from the stack, the first method call inserted is popped off last and hence the value is just +1. Which means each method call is holding a separate stack and changes done to the variables are stored and returned.
Then it looks like it is behaving as call by value and not as call by reference.
Is there any limitation to java on these objects?
Anitha Ramaiah wrote:
As string literals values will be maintained in a string pool and the variable we create are reference to those values, in the example I have provided, the variable fromDate got changed and so the expectation is finally it should return the last modified value.
Anitha Ramaiah wrote:
As string literals values will be maintained in a string pool and the variable we create are reference to those values, in the example I have provided, the variable fromDate got changed and so the expectation is finally it should return the last modified value.
Example:
initial: fromDate - 21-02-2014 [Now the reference fromDate is pointing to the string object 21-02-2014]
after first check: fromDate - 22-02-2014 [Now the reference fromDate is pointing to the string object 22-02-2014 and ofcourse 21-02-2014 also exists in the pool]
after second check: fromDate - 23-02-2014 [Now the reference fromDate is pointing to the string object 23-02-2014].
Now when returning, it should give 23-02-2014. But is not. It is returning 22-02-2014.
Why I have specified the work recursive functions here is, when I look into the thread stack, it looks like below.
Thread [main] (Suspended)
CalcDates.getNextWorkingDt(String, List<String>) line: 119
CalcDates.getNextWorkingDt(String, List<String>) line: 117
CalcDates.getNextWorkingDt(String, List<String>) line: 117
CalcDates.main(String[]) line: 46
So when returning the value, the last method call(marked in bold), which is inserted first into the stack is holding the initial value and hence it is getting returned.
Why is it so? Does the string value specific to one method call? I mean is it treated like a local variable? If so why?
Anitha Ramaiah wrote:I understood and I accept.
But the same piece of code is returning(yes, I mean return) 23-Mar-14(I mean the actual answer), when called from a web service. If so is there any difference between jvm stack for a standalone and web application or is it the container's behavior to handle this?
Anitha Ramaiah wrote:That is why I am confused. The same code is running without an explicit return or an assignment statement even in production environment now.
Consider Paul's rocket mass heater. |