aspose file tools*
The moose likes Beginning Java and the fly likes WRAP class question and nested loop question!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "WRAP class question and nested loop question!!" Watch "WRAP class question and nested loop question!!" New topic
Author

WRAP class question and nested loop question!!

huanyu zhao
Greenhorn

Joined: Feb 22, 2001
Posts: 29
18.

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 ]

[img]http://www.flyheart.com/bbs/attach/2002/11/07/59790-signature-embed.jpg[/img]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

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...
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
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.
huanyu zhao
Greenhorn

Joined: Feb 22, 2001
Posts: 29
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: WRAP class question and nested loop question!!