This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How about with java.util.Stack?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How about with java.util.Stack??" Watch "How about with java.util.Stack??" New topic
Author

How about with java.util.Stack??

Hades Pan
Ranch Hand

Joined: Nov 25, 2001
Posts: 106
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: 1242
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
"java pass by value"
I couldn't understand this
Can you explane it o lot? Think you ~~!
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
Try read this... it is very good.
http://www.javaranch.com/campfire/StoryPassBy.jsp
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How about with java.util.Stack??