This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
what is the result? A. compile fail B.print out "0" C.print out "3" D.compile succeded but exception at line 3 whythe answer is B not C!?? 36.
what is the result? A.i=1,j=-1 B. i=0,j=-1 C.i=1,j=4 D.i=0,j=4 E.compile error at line 4 how can i read this kind of nested loop faster??? Edited by Corey McGlone: Added code tags [ June 24, 2002: Message edited by: Corey McGlone ]
Ok, for the first question, you have to keep in mind that when you assign a new object to the parameter, you reference a new object, not the same one that was passed in. Take a look at this animation to get a better understanding of how this works. As for the second question, what are you asking? Are you having a problem understanding the question, or is it just taking you a while to figure it out? Corey
what is the result? A. compile fail B.print out "0" C.print out "3" D.compile succeded but exception at line 3 whythe answer is B not C!??
Okay, the only way you can fiddle around with an object is through its reference. However, the reference and the object being referenced are two different things. When you passed the Integer reference i into add3() you are actually making a local copy of the reference i, which is still pointing to the same object (with intValue 0) being referred to by the original reference i left behind in main(). Any changes made in this object through the *local* reference i would have been permanent, had there been any. BUT, you made the *local* reference i point to a new Integer object now (with intValue 3), one that is NOT referenced by the original reference i left behind in main(). When add3() returns, the *local* reference i goes out of scope, and the object it was referencing is now eligible for garbage collection. The original i is still pointing to the one with intValue 0 so when add3() returns...
For the second question, I'm assuming that you want to understand the code faster, i.e. make it more readable. It would really help if you indent your code blocks, like so:
Basically, what happens is that the outer loop (with the label tp) does nothing, coz the inner loop just decrements j until it becomes less than i, in which case it just breaks out of both loops. The output would be similar to this: i=0,j=-1 Hope that helps.
Joined: Feb 22, 2001
really thanks to all!!! coz i am a chinese guy so my english is not good enough,and it`s hard to understand. but you---warm hearted men just guess my words! then answer me ! realy thank u!