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?

Andrew Tobilko
  • 44,067
  • 12
  • 74
  • 128
pranav shukla
  • 343
  • 3
  • 14
  • 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](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – SomeJavaGuy Sep 01 '16 at 07:26

5 Answers5

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. */

  • So is it that while pushing into the stack , a new reference is generated? – pranav shukla Sep 01 '16 at 07:34
  • 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. –  Sep 01 '16 at 07:36
1
stack.push(current);

enter image description here

current = current.left;

enter image description here

Andrew Tobilko
  • 44,067
  • 12
  • 74
  • 128
  • 1
    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. – pranav shukla Sep 01 '16 at 07:51
  • @pranavshukla, `currentInStack` is initially assigned to `null`, then, when you call the method, `currentInStack = current` – Andrew Tobilko Sep 01 '16 at 08:27
0

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

OldCurmudgeon
  • 60,862
  • 15
  • 108
  • 197
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.

V-master
  • 1,580
  • 12
  • 18
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.

Sergei Rybalkin
  • 2,890
  • 11
  • 24