Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Arguments

 
Vishnu Munnangi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
This is the question from Dan's mock exam,
please go through the code..

What is the result of attempting to compile and run the program?

a. Prints: 1,1
b. Prints: 1,3
c. Prints: 3,1
d. Prints: 3,3
e. Run-time error
f. Compile-time error
g. None of the above

The answer given is a, but i am not able to understand how 1,1 is printed.
Can any one explain this, appreciate your time
Thanks

(top secret [C0DE][/C0DE] tags added)
[ October 15, 2004: Message edited by: Barry Gaunt ]
 
Saheed Adepoju
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok
If i understand the question well, the values of the arrays are copied unto the other ones during the assignment of the first one to the others. So printing out the values result in the printing of the new replaced information. Bottom line is this when u pass arrays u are sending not a copy but the actual parameters which results in the change. Hope u understand. Fellow programmers plz correct me if i am wrong....
Saheed Adepoju
SCJP 1.4(Coming real soon..)
Imagination is better than knowledge..
 
Louie van Bommel
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GFC306.i1 = {1}
GFC306.i2 = {3}

Now we call m1(i1):

in method m1(), we have a LOCAL variable i1 which is a copy of i1; I'll call it (m1)i1.

int[] i3 = i1;
We create another LOCAL variable i3 which i'll call (m1)i3 (just for fun )
(m1)i3 points to (m1)i1 which points to that {1}

i1=i2;
does absolutely nothing we are interested in because it's just a LOCAL copy of the static variable.

i2=i3;
causes GFC306.i2 to point to (m1)i3 which points to that {1}

Net result:
GFC306.i1 was never changed at all because in m1() it didn't change GFC306.i1 but it assigned a value to (m1)i1 instead.

and
GFC306.i2 is pointing to that {1}

I had to work thru this about 6 times to not come out with 3 and 3
 
Vishnu Munnangi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much Louie for your detailed analysis of the code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic