Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

WRAP class question and nested loop question!!

 
huanyu zhao
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic