File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Arguments" Watch "Method Arguments" New topic

Method Arguments

Vishnu Munnangi
Ranch Hand

Joined: Sep 28, 2004
Posts: 114
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

(top secret [C0DE][/C0DE] tags added)
[ October 15, 2004: Message edited by: Barry Gaunt ]
Saheed Adepoju
Ranch Hand

Joined: Jun 23, 2004
Posts: 267
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..

Saheed Adepoju<br />SCJP 1.4<br />SCJD (B&S..In progress)
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
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}

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

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.

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

Joined: Sep 28, 2004
Posts: 114
Thanks very much Louie for your detailed analysis of the code.
I agree. Here's the link:
subject: Method Arguments
It's not a secret anymore!