aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Variables - Values ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Variables - Values ?" Watch "Variables - Values ?" New topic
Author

Variables - Values ?

Aru
Ranch Hand

Joined: Jul 20, 2000
Posts: 112
The output of this code is 1 0 1......Can any one explain the output of the program. ?

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

An explanation is appreciated.
Thx in Advance.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Aru,
I think the reason b's value was not changed in method call
g(b, c) is because b is passed by value whereas c's value is
changed because c is passed by reference(c is a reference to
an int array).
regards
Rajan Narayanan
Greenhorn

Joined: Aug 07, 2000
Posts: 16
public class Test {
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;
}
Hi Aru !
i also went thru the piece of code but when i take first look i also thgt the way u thgt when take a closer look it seem the variable b is in the argument list of method g so it becomes local to the variable where else c[] is passed by reference and a is instance variable so it can be accessed all the methods in a class. so tha output is 1 0 1 is right.
Hope ur doubt is cleared...
thanx
Rajan
R
public static void main(String args[]) {
Test obj = new Test();
obj.f();
}
}
Aru
Ranch Hand

Joined: Jul 20, 2000
Posts: 112
Thanks chengx & Ranjan for ur reply & time.
I got it clear now.
Aruna
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
It still is not clear for me,
can someone please explain it clearly..,
bcs a and b are instance variables of class, when u pass it to
the method, u only pass a copy of it, original one will not
be changed.,
so a should be 0 and b should be 0 and c should be 0..,
i know i am wrong, i can't understand what u guys said is
correct,
can someone explain in simple terms


Bhasker Reddy
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
b is a primitive data type and a copy of value is passed.
You can access and change the instance values from the methods. in your case local variable(parameter) b shadows the instance variable b and hence change in value of b is not reflected while a is not shadowed and hence changed value 1 is printed.
c is an array of Integer i.e Object and a copy of reference is passed as a parameter. Although you are using the same local variable name c , but it is refering to the same array as in instance variable c , hence changing the value of the Array refelects in printing.
Hope it must have cleared ur doubt.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: Variables - Values ?
 
Similar Threads
Reg. Reference datatypes in method calls
need help
Pass By Value Problem
Question from khalids sample paper
Assignments