• 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

Hi

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have code like this,

public class Foo{
int a;
int b;

public void f(){
a=0;
b=0;
int[] c={0};
g(a,b,c);
System.out.println(a+ "" + b +c[0]);

public void g(int a,int b,int[] c){
a=1;
b=1;
c[0]=1;
}

public static void main(String[] args)
{
Foo obj=new Foo();
obj.f();
}

Whate could be the output of the following program
i expected 1 1 1 [even though a nd b passed by value but a ,b are
common to both ]
but its printing 101;
please explain
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Please choose a meaningful title for your topic.
2. Please use [B][/B] tags to enclose your properly indented code.
Thanks
[ June 25, 2007: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sudharsan tettu:
Whate could be the output of the following program
i expected 1 1 1 [even though a nd b passed by value but a ,b are
common to both ]
but its printing 101;
please explain



I am surprised if it printed 101. It shouldn't have.

I should print 001, becuase primitives are passed by value and objects are passed by reference.

Hence a and b values will not change after being passed to the method g(), but since c is an array (and hence an object), it is passed as reference and the change in the value in g() is reflected in f().
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code has 2 syntax errors and if you correct those it's printing "001".
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudharsan,

I get the following output:

001

The best way is to insert a System.out.println(a + "" + b) statement in the method public void g(int a, int b, int[] c).
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

even though a and b passed by value but a ,b are
common to both



No, a and b are local variables in

and are not common to anything else.
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can any elaborate on the above program as I didnt, the reference concept of object and variables .
[ June 26, 2007: Message edited by: Divya Gehlot ]
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Divya Gehlot

What are you so confused of ki striking your head with the wall..

See these three lines

int a;
int b;

int[] c={0};


Here a, b are of integer primitive data type..
while c is a reference to an array of data type int(means reference to an object, right)

OK..

Now when you are passing them to method g(int a,int b,int[] c)
a and b are passed by value
c is passed by reference (means a copy of reference c will be created which will refer to the same object as refered by c in method f() i.e. the array object you created in line int[] c={0};)

Now you are having two references referencing to the same object..
If any reference changes the object, that will be reflected when you will see the object by other reference..

And in method g(int a,int b,int[] c),
you are changing the object by having c[0]=1;

While there will be no effect of changing the primitive variables in method g(int a,int b,int[] c), since they are unrelated to each other..And there scopes are also different..

So for the line
System.out.println(a+ "" + b +c[0]);
in method
f()

you will get an output of 001..

Regards..
 
sudharshan tettu
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for example if we change the method signature like this and call...its behaving weird...

public class Foo{
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)
{
Foo obj=new Foo();
obj.f();
}

This time it prints this 101
Now the value of a is changed but where as b has not

explain please
 
khushhal yadav
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sudharsan tettu

when you are doing this

public void g(int b,int[] c){
a=1;
b=1;
c[0]=1;
}



You are changing the value of the instance variable a.
as there is no local variable a to method g(int b,int[] c)

Hence in method f()
when you are writing
System.out.println(a+ "" + b +c[0]);
in method
f()
you are getting
101

as there is no local variable a in method f()..
It's only the instance variable a which is there..
And it's value has changed in method g(int b,int[] c)
OK..

Regards..
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Khushhal

as there is no local variable a in method f()..
It's only the instance variable a which is there..
And it's value has changed in method g(int b,int[] c)



But the code shown by Sudharshan is below it has variable a in f() which is set to 0 ie a=0;so i think that it is local variable of method f();
i am not sure about what you exactly mean by local variable here please ellaborate it.


 
khushhal yadav
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dhwani mathur

See We have declared the variable a outside the method f()
There is no decleration of variable a in method f()..
which means it's an instance variable in method f() which we are referring..
initially by default initialized to 0 at the time of instantiation of the class..
Scope is the entire class..right..

While now see the signature of method g(int b,int[] c)
You are declaring a variable b by having int b as an arguement..
Its scope is only within method g(int b,int[] c)..And it has shadowed the instance variable b..
It's the local variable b which we are having in method g(int b,int[] c)..And hence when we are doing b=1; in method g(int b,int[] c), it's the local variable b which is getting changed to 1..Instance variable b is still 0..If you want to change instance variable b, you can write this.b = 1 in method g(int b,int[] c)..Then you will give you an output of 111..

While when we are doing a=1; in method g(int b,int[] c), it's the instance variable a which is getting changed to 1, as there is no local variable a in method g(int b,int[] c) now as it was there in the earlier case of g(int a,int b,int[] c)..

So when we are doing
System.out.println(a+ "" + b +c[0]);
in method
f()
we are printing instance variables, as there are no local variable(neither passed as an argument or declared inside method f()) which are shadowing the instance variables in method f()..

And you are getting 101 as answer..

Regards..
 
sudharshan tettu
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks kushal your explanation cleared my doubt
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for such a easy to understand explanation khushhal.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic