• Post Reply Bookmark Topic Watch Topic
  • New Topic

Could you pleast explain the below  RSS feed

 
S.Reddisekhar Reddy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am expecting to print
size:5
weight:5

but it is printing as
size:0
weight:0

I have debug this code identified that while returning s it is again executing the test(k) and the falg becoming as true and s as Sample object with size=0 and weight=0
could you please explain behavior?

Thanks
Reddy
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does line 30 do? It calls a method that returns an instance of Sample that you wish to see printed. What happens to this object?

Also, please UseCodeTags ← click
I added them for you this time. Doesn't it look better?

Also, this:can be replaced by this:
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S.Reddisekhar Reddy wrote:
I have debug this code identified that while returning s it is again executing the test(k) and the falg becoming as true and s as Sample object with size=0 and weight=0
could you please explain behavior?


Basically, you are returning the wrong Sample object back to the main() method. It is true that the recursive test() method call does create a Sample object (that you want), but that object doesn't get to the main() method.

Henry
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The break statements in the middle of the for and while loops make it quite hard to follow the flow of the program. Have you thought about having more than one condition in each of the loops instead? So long as the statement can be evaluated to true or false then it will work and might help other readers understand what is going on a bit better.



Although, the result is always the same no matter what number is given in the message sent to test, so depending on what you are trying to achieve you could potentially make it much simpler by instantiating a new Sample object and setting the values of the fields directly.
 
S.Reddisekhar Reddy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, i achieved the expected by creating the object as instance variable, i am confused that why the control is going to test(k) while returning the object.
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S.Reddisekhar Reddy wrote:... i am confused that why the control is going to test(k) while returning the object.


Take a look at the value you are passing to test in your main method and compare that to the conditions each of your loops is checking for to determine whether to continue to execute or not. Does this help you to see why test(k) is being called?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!