• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

how this output is got

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will be written to the standard output when the following program is executed?

the answer for this question is 1 0 1. i dont know how they are gettin this output please explain this program and howwe are gettin the out put.
[ September 09, 2004: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a = 1 because it is set to 1 in method g

b = 0 because variable b is being shadowed in method g, it's not the instance variable b that is being set to 1, but the local (method) variable b is being set to 1

c is also being shadowed in method g, BUT both reference variables refer to the SAME array object. so when c[0] is set to 1 in method g, the array that the instance reference variable c is pointing to will also be affected, because it's the SAME array. so c[0] = 1

than they are concatenated which gives the output 1 0 1
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are testing you on how variables are passed. All variables are passed by value- but with an object the reference itsellf is a value that is not changed but the thing the object points to CAN change.

So we start out with obj. When it is created, I believe that a and b are initialized to zero (should check this actually, but I am fairly certain of it). I am going to call these obj.a and obj.b

Now the call is made to f. Here, obj.a is set to zero, and obj.b is set to zero. The local array c, which I will call f_local.c, has its zeroith element set to zero. So this is f_local.c[0] = 0;

Now the call is made to g(b,c).

Both are passed by value- so b itself is not taken up, a copy of b is taken. We'll call this g_copy.b. Now for c, the reference itself, essentially a pointer, is passed by value. However, THE ARRAY ITSELF WHICH IS POINTED TO IS NOT COPIED. Thus the reference g_copy.c points to f_local.c.

Now a = 1. This is obj.a = 1
b is set to 1. This is g_copy.b = 1. This is thrown away as soon as the function returns.
Now g_copy.c (pointing to f_local.c) sets the zeroith element. This is equal to f_local.c[0] = 1.

So when it returns and prints a, b, c it prints

obj.a (set to 1)
obj.b (still at 0 never changed)
f_local.c[0] (changed in function to 1)

I apologize if my explanation is not as lucid as it might be, a better nomenclature for passed values would help.
 
Selva Prasad Rajendran
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you jimmy u have cleared my doubt thank u very much
 
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
within the method g, you have a local variable b that masks the member variable b. if you re-wrote the method like this:


would it be clearer? but if you wrote this:


you'd get something different.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tom Tolman:

So when it returns and prints a, b, c it prints

obj.a (set to 1)
obj.b (still at 0 never changed)



Just a question for Tom. I thought the print statement required it to print the function's copy of a and b and not obj.a and obj.b. Am I missing something? I don't doubt the answer but I got 0,0,1 after a quick glance at the problem. Now I'm interested.
 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a question for Tom. I thought the print statement required it to print the function's copy of a and b and not obj.a and obj.b. Am I missing something?

We are probably talking sideways to each other, but let me try to clarify what I am saying.

It sounds like you are interpreting A and B to be separate instantiations within the function. This would be correct if they were declared there, and were shadowed variables. (int A = 0; int B = 0 However, they are not declared there, so they are actually using the instance variables (the instance of the object owns these variables).

Does that make sense?
 
Louie van Bommel
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tom Tolman:
This would be correct if they were declared there



Yup. I see now that they were not declared there, I mistakedly read them as also being declared in the function. Thanks for the clear-up
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic