The following Quession's ANS is A but why? import java.util.Stack; public class aClass { public static void main(String []agrs) { Stack st1 = new Stack(); Stack st2 = new Stack(); new aClass().Method(st1, st2); System.out.println("st1 has: " + st1); System.out.println("st2 has: " + st2); } private void Method(Stack st1,Stack st2) { st2.push(new Integer(100)); st1 = st2; } } A. print st1 has: [] st2 has: [100] B. error in st1 = st2 C. error in ("st1 has: " + st1) D. print st1 has: [100] st2 has: [100]
Fei Ng
Ranch Hand
Joined: Aug 26, 2000
Posts: 1241
posted
0
becuase st1 = st2; in Method() is gone after the method. remember, java pass by value? main() pass st1 and st2 to method() st2 get pushed a value "100"; since st2 pointing to the same stack object thats in main() so the value stay after the method. st1 = st2; since it is a copy. It won't matter after the method. remember, the references got copied and passed to method(). change the references doesn't matter but change what is in the object will stay.
Hades Pan
Ranch Hand
Joined: Nov 25, 2001
Posts: 106
posted
0
"java pass by value" I couldn't understand this Can you explane it o lot? Think you ~~!