Page 1 of 1
About Question enthuware.ocajp.i.v7.2.974 :
Posted: Mon Aug 13, 2012 1:25 am
by georgeferreira
The code on the question says:
Code: Select all
import java.util .*;
public class TestClass{
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")); //assume that the method push adds the passed object to the stack.
x2 = x1;
}
}
A space is set between "util" and ".". It will compile? I marked "None of above".
Re: About Question enthuware.ocajp.i.v7.2.974 :
Posted: Mon Aug 13, 2012 1:32 am
by admin
Although space is not needed (and not recommended either) here, it will not cause any compilation issue. It will compile fine.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.974 :
Posted: Mon Aug 13, 2012 1:52 am
by dtchky
Output after compilation:
Stack@424c2849 Stack@15e8f2a0
The code is correctly printing out the references to both stack objects.
Question Bank: 1/15
georgeferreira is correct,
none of the above, but for a different reason, at least on my machine anyway
Code: Select all
java -version
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)
Re: About Question enthuware.ocajp.i.v7.2.974 :
Posted: Thu Aug 16, 2012 2:04 am
by admin
It seems the toString() method of Stack implementation has changed. The question statement has now been changed to "What will be the contents of s1 and s2 at the time of the println statement in the main method of the following program?" to avoid this issue.
thank you for your feedback!
Re: About Question enthuware.ocajp.i.v7.2.974 :
Posted: Wed Sep 26, 2012 1:38 pm
by javanaut
If x1 and s1 point to the same memory location and are modified when x1.push() is called why is s2 not modified when local variable x2, has same memory address as s2, is assigned to local variable x1, which has the same memory address as s1?
Is it because x2 would need to explicitly call a method such as push from the Stack class in order to modify the object that both x2 and s2 point to?
Re: About Question enthuware.ocajp.i.v7.2.974 :
Posted: Wed Sep 26, 2012 2:46 pm
by admin
javanaut wrote:If x1 and s1 point to the same memory location and are modified when x1.push() is called why is s2 not modified when local variable x2, has same memory address as s2, is assigned to local variable x1, which has the same memory address as s1?
I am not sure what you mean. x2 is not assigned to x1 in the given code. x1 is being assigned to x2 and that means, x2 starts pointing to the same object as x1, which is the same object as being pointed to be s1.
So why would the object pointed to by s2 change?
I suggest you to read this article thoroughly:
http://www.javaranch.com/campfire/StoryPassBy.jsp
Also, try to think of variables and objects as separate. "Changing x1" and changing "the object pointed to by x1" are two different things. Further, variables don't have methods. The You seem to be mixing that up.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.974 :
Posted: Thu Sep 27, 2012 11:19 am
by javanaut
Hello Paul,
Thank-you sir for the response. You are right, x1 is assigned to x2 in the program in question. I mixed these up when writing my reply.
I understand that object references are passed by copy to an extent but I do not understand why x1 can change s1 but x2 can not change s2. The only different is that x1 calls a method from Stack's API.
If x1 can point to s1 and change its value why does x2 pointing to x1 and s1 not change s2's value?
This is interesting to think about in your response, "...variables don't have methods".
I guess Ill read the JavaRanch article again. Thank-you for the help. This program does not seem logical to me and programming is all about logic.
Respectfully,
javanaut
Re: About Question enthuware.ocajp.i.v7.2.974 :
Posted: Wed Dec 12, 2012 5:10 pm
by ksnortum
x1 isn't "changing" s1. x1 and s1 both point to the same object reference. When you execute "x2 = x1" you are not saying "s2 is now the same as s1", you are saying "x2 now points to the same object as x1." The local variable x2 goes out of scope when you exit prosessStacks() and s2 remains unchanged.
(BTW, I got this question wrong too and from what I've read, even seasoned Java programmers still get confused about this.)
Re: About Question enthuware.ocajp.i.v7.2.974 :
Posted: Thu Jan 10, 2013 1:15 pm
by guest
small typo error..
In the explanation .."You created two objects in main method: s1 ------------> [ EMPTY ] STACK 1 OBJECT s1 actually contains 15000 (say) s2 ------------> [ EMPTY ] STACK 2 OBJECT s1 actually contains 25000 (say)"
the last line shld be "s2 actually contains 25000 (say)."
Thanks.
Re: About Question enthuware.ocajp.i.v7.2.974 :
Posted: Fri Mar 08, 2013 9:41 am
by The_Nick
dtchky wrote:Output after compilation:
Stack@424c2849 Stack@15e8f2a0
The code is correctly printing out the references to both stack objects.
Question Bank: 1/15
georgeferreira is correct,
none of the above, but for a different reason, at least on my machine anyway
Code: Select all
java -version
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)
Hi,
Just to update things a bit. As of now, running on a java version "1.7.0_17", it prints out the actual content of the Stack objects, hence: [100] []
The_Nick
Re: About Question enthuware.ocajp.i.v7.2.974 :
Posted: Tue May 28, 2013 8:14 am
by bptoth
guest wrote:small typo error..
In the explanation .."You created two objects in main method: s1 ------------> [ EMPTY ] STACK 1 OBJECT s1 actually contains 15000 (say) s2 ------------> [ EMPTY ] STACK 2 OBJECT s1 actually contains 25000 (say)"
the last line shld be "s2 actually contains 25000 (say)."
Thanks.
This one still seems to need correction
Re: About Question enthuware.ocajp.i.v7.2.974 :
Posted: Tue May 28, 2013 7:41 pm
by admin
Fixed.
thank you for your feedback!