• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java stack in recursive functions

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi All,

I am really confused with recursive functions in java stack.

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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?




First, read this... https://coderanch.com/how-to/java/CallByReferenceVsCallByValue

Basically, this has nothing to do with recursion. And as you suspected, it has to do with how parameters are passed.

Henry


PS.... welcome to the ranch
 
Anitha Ramaiah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick reply.

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?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.




Please read the link that I provided again.... basically, Java passes objects by passing a copy of the reference. So, if you change the object (mutable object of course), the changes will be seen by the caller. But if you change the reference, the changes will *not* be seen by the caller. This is a really subtle distinction that should be clearly understood.

Henry
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
… and welcome to the Ranch
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In reading your response again...

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?




I noticed that you keep using the word "return" -- meaning related to the return keyword. If that is true, then take a look at the recursive call -- while you are actually returning the object in the called method, the caller method isn't saving the returned value anywhere.

Henry
 
Anitha Ramaiah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Sorry, that is not possible. Your code is ignoring the return. The same code in a Web Service should do exactly the same thing.

If you don't want to ignore the return, you will need to change this line ...



to this ...



Henry
 
Anitha Ramaiah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is why I am confused. The same code is running without an explicit return or an assignment statement even in production environment now.

In standalone application, now I changed the to . So it is working. Even is working.

But in our prod environment it is working without that return or assignment statement.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.




Don't know what to tell you -- but you clearly have to figure out why. IMHO, something that should not be working, but does work is much *worse* than something that should be working, but doesn't. In later case, it is merely a bug and you have incentive to find it. In the first case, it is likely multiple bugs (or a design flaw), that happen to cancel each other out. This is very bad -- as you are running on luck (and you don't know why).

The JLS is very clear on how parameters are passed, and how they are returned -- and there isn't wiggle room for different implementations to behave differently. You need to figure out why it works in production.

Henry
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic