GeeCON Prague 2014*
The moose likes Mock Exam Errata and the fly likes passing a reference as an argument to a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "passing a reference as an argument to a method" Watch "passing a reference as an argument to a method" New topic
Author

passing a reference as an argument to a method

Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
The Rules Roundup question asks, "When you pass a reference to an array, as an argument to a method call, you pass a copy of the original array?" The given answer is true. No problem.
The question's meaning suggests, to me, that the three println() methods below should output 1, 32, and 1 - if the code was a correct demonstration. The actual output is 1, 32, 32. What would be a correct code demonstration of the question?

Thanx
[This message has been edited by Steven YaegerII (edited May 26, 2001).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Steven,
I don't remember the exact wording of the question; but when you 'pass a reference to an array' you are actually passing a copy of the reference, not a copy of the array... which is why you are seeing an output of '1,32,32' in your example.
Every time you call a method in Java, the JVM creates parameter variables. If the argument is a primitive type, a copy of the original value is passed ie a copy of the original variable's value is passed. Any changes made in the method affect only the copy, not the original value.
If the argument is a reference type, a copy of the original reference is passed. Array's are handled as objects, not primitive types; therefore, the parameter variable's value equals the memory address of the original object ie the parameter reference points to the original object and any changes made in the method affect the same object.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
Thanks a million, Jane. I copied all the RR questions and put them to paper & re-numbered them (1,2,...144), which is why I didn't list a question number. Your explanation makes perfect sense. I forgot that arrays are treated as objects. I'm going to go see if the question's wording has been fixed since I copied them. Thanks again.
I just checked, and the question is correct with your explanation. I must've worded it wrong.
[This message has been edited by Steven YaegerII (edited May 28, 2001).]
 
 
subject: passing a reference as an argument to a method