1

I was doing inorder traversals when in the solution I came across the following lines:

stack.push(current);
current = current.left;

Now my question is that when I push current into the stack and make current = current.left; then will the node that is there within the stack also change to current.left? In my case, the current in the stack still points to the original current but the current variable does point to current.left. Why is this?

1
  • because current seems to be a local variable. If you push current into your Stack it will contain the reference to current. But as current is also a variable you´re changing the reference current is refering to. But that wont magically chence the reference you did push into the Stack. Check this SO question for further details Commented Sep 1, 2016 at 7:26

5 Answers 5

1
stack.push(current);

enter image description here

current = current.left;

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

So basically the reference current is passed to push by value. So currentInStack is generated when we pass reference current in push. Thanks for this elaborate art.
@pranavshukla, currentInStack is initially assigned to null, then, when you call the method, currentInStack = current
1

You're changing what the variable "current" references, but the object in the Stack won't be changed just because your variable is now referencing a different object.

/* Misread the question ignore this...

If you're asking if the changes you make to the object 'current' will also have the same effect as the current object pushed to the stack. The short answer is yes.

You're pushing the object, not a clone. The object never changes or is cloned, and putting something in a Stack is just another way including your variable declaration to reference the object in memory.

The same applies to HashMap, ArrayList, etc. */

2 Comments

So is it that while pushing into the stack , a new reference is generated?
When pushing something to the stack what you're doing it just adding the object, the only way you reference that object is via the variable, "current". After pushing it, you change what the variable current references to, but that doesn't change what was previously pushed to the stack.
0

At a guess - because stack.push(current) is taking a copy of current for the stack.

Comments

0

just look at it from other perspective: in C/C++ you have pointers. All variables in java should be threated like those pointers. When you are calling a method, in fact you pass the new pointer to given object, rather than reference to it. With = you are just overriding value of pointer with pointer to new object, and old object still exists till garbage collector will collect it.

Comments

0

Java is pass-by-value. You pass the value of reference to object. Changing the reference will not affect copied reference value in stack.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.