I am studying the certification book by Khalid A Mughal. I have a question from sample question 37 in this book, still don't understand the printout. Please give me a help. Thanks in advance. The code is public class Qcb90{ int a; int b; public void f(){ a=0; b=0; int[] c = {0}; g (b,c); system.out.println(a + "" +b+"" +c[0] + ""); } public void g (int b, int[] c){ a=1; b=1; c[0]=1; } public static void main (String args[]){ Qcb90 obj = new Qcb90(); obj.f(); } } the print out is 1 0 1. please give me a explanation. Thank you again. Haijun
Stefan Seeba
Greenhorn
Joined: Oct 24, 2000
Posts: 5
posted
0
Hello Haijun We deal with three different things here: 1) a is a member of Qcb90 and therefore also known in g(...). The a=1 is valid for the whole scope of the class. 2) b=1 in g(...) deals with the method-local-variable in g(...). Therefore the value 1 is lost when the method returns. 3) c is an array and therefore a reference-variable. This reference is given to g(). The g()-method-local c "points" to the same c as the f()-method-local-variable c. Therefore c[0]=1 changes the same memory-position, that is also referenced by the f()-variable c[0]. I hope, this helps, Best regards from Hamburg, Stefan [This message has been edited by Stefan Seeba (edited October 31, 2000).] [This message has been edited by Stefan Seeba (edited October 31, 2000).]
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Hi Haijun, I have tried to comment the code to help you understand how it works:
Primitive and object reference types are always passed by value. The result of line 21 might seem surprising, since you had the impression that the whole array was passed by value. It is not the case. The member variable <code>c</code> contains a reference to a object (which is an array). The copy of this reference is given to the method <code>g</code>. The parameter <code>c</code> in method <code>g</code> now contains a reference to the same array as the member variable. This is why c[0] in g access the same element as c[0] in any other method of class Qcb90. To make sure you understand this concept, change the line 21 in the code to be: <pre>21. c = new int[] {1};</pre> Now compile and run the program again. The result should be: <pre>1 0 0</pre> This is because only the copy of c is altered in the method g, hence no modification has been made to the member variable c. Got it? Regards, Beno�t
haijun wang
Greenhorn
Joined: Aug 18, 2000
Posts: 21
posted
0
Hi My friends: After your detailed explanations, I truely understand that what is the meaning of "pass by value". Thank you very much. Looking forward to get your help. Haijun
Helen Yu
Greenhorn
Joined: Jul 13, 2000
Posts: 29
posted
0
Hi, there: You can use the SEARCH utility of this salon, it works perfectly! In fact, you will meet lots of basic problems in the exam for this "Pass by value" topic, search the topic here, you will accelerate your preparation speed for the exam. Hope this help and good luck to you. I cleared this exam on Oct 9th with 86%. hope this help
Can anyone tell the difference between int c[] = new int [] {2}; and int[] c = {3}; They both will create array objects....so give the same result when i compile...ref Benoit d' �ncieu�s post.......somehow i find this concept real hard.....urghhhhhhh Faiza
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi Faiza, Both your examples create an array with one element.
The first example stores the value '2' in c[0] and the second example stores the value '3' in c[0]. When you are working with arrays, code in {} acts as an initializer. In both cases you've declared an array with no dimensions and initialized it with one value which has the effect of creating a one dimensional array with one element. Try the following code to see what happens: