• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stack Doubt.  RSS feed

 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gurus,

When I compiled the following program and ran it, it gave the output

[100] [].

But I expected [100] [100].

Can anybody explain this pleeeeeeeeeeese.

Thanks in advance.

Suresh.


import java.util .*;
public class Stack01
{
public static void main(String args[])
{
Stack s1 = new Stack ();
Stack s2 = new Stack ();
processStacks (s1,s2);
System.out.println (s1 + " "+ s2);
}
public static void processStacks(Stack x1, Stack x2)
{
x1.push (new Integer ("100"));
x2 = x1;
}
}
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java the arguments to a method are passed by value.
What does that mean when you pass reference to a object(in this case Stack object)?

A copy of the references s1 and s2 is passed to the method. These references point to the same objects as the original references.

So any change to the references themselves is change to the copy
of the references only...these changes are lost outside the method.

Thus when you do
s2 = s1 inside the method...this is going to point the copy of
s2 reference to a copy of s1 reference inside the method.

When the method returns , the copies are lost as they were local to the
method and now s2 points back to stack s2 which is empty.

Stack s1 on the other hand has one element as we pushed something inside it
which modified the stack object.

Let me know if it isnt clear...

Thanks,
Megha
 
dornala reddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

You are getting the o/p as [100][] beacause i explained it why it will happenning.

1. while executing 'processStacks (s1,s2);' nothing inside the two stacks.
and we are passing s1,s2 as arguments. Here stack objects are referred
by s1 and s2.

2. while calling the method the parameters x1 and x2 takes the values
whatever is there inside the argumets s1 and s2. So x1 and x2 get the
references of the two stack objects.
Here the 2 stack objects are pointed by 4 variables.

3. Inside the method we added one Integer object into One stack and it is
referred by x1 and s1.
Here, inside the second stack no objects and referred by s2 and x2.

4. After executing 'x2=x1;' you are assigning the reference, what is there
inside the x1(it is first stack object which contains the Integer
object),to x2.
Here first stack object referred by 3 variables(s1,x1,x2). Inside
second stack object no Ineteger object.

5. Method execution is over. You are printing the stack s1 and s2.
Here s1 stack object contains Integer object.
No object inside s2 stack object.
So you got that output.

To get the required output you have to add the stmt 's2=s1;' inside the main method.You may or maynot delete the stmt 'x2=x1',it gives you the required output.

I hope you clear.
This is as part of my knowledge.
If you already know the better answer than this please post.

With regards,
dornalareddy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!