Win a copy of Modern frontends with htmx this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pass by value?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

I have some notes on arrays posted at http://webhome.idirect.com/~jgriscti/lang/arrays.html and some on method invocation posted at http://webhome.idirect.com/~jgriscti/oper/methods.html
Hope they help.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiousity.
-- Dorothy Parker
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic