All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.
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".
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)
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!
If you like our products and services, please help us by posting your review here.
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?
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.
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.
If you like our products and services, please help us by posting your review here.
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.
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.)
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] []