aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing Value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing Value" Watch "Passing Value" New topic
Author

Passing Value

Rex Rodriguez
Greenhorn

Joined: Jan 16, 2002
Posts: 21
It prints bArr[0] value as 1 but the value of a is printed as 0. It should
increment the value of a too, right? Can someone explain?
public class test {
public static void main(String args[]) {
int a=0; int b=0;
int[] bArr=new int[1]; bArr[0]=b;
inc1(a); inc2(bArr);
System.out.println("a="+a+" b="+b+" bArr[0]="+bArr[0]);
}
public static void inc1(int x) {x++; }
public static void inc2(int[] x) {x[0]++;}
}
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
wrong. it doesnt increment the value of a.
the difference is the whole idea of pass by value and pass by reference.
try seraching this forum and ull find tons of discussions about it (some more complicated than others) but the main idea ALTHOUGH it isnt really accurate is that primitives variables like int , char etc are passed to fucntions by value (meaning they are copied to their counterparts in the function) while objects including arrays are passed by refrence so u can change the object u send inthe function.
you will see in the forum that it is actually more complicated than that and that both are passed by values only in the second time with the arrays we pass references by value.
like i said a bit complicated at first.
try reading some more posts around here because im sure some moderator explained better than me.
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
there was a thread posted by self, exactly the same thing was dicussed. called as pass by refernce and pass by value.
objects are passed by reference. And array are objects.
so when you call the method inc2(bArr) you are actually passing a reference to the object. This is different to when you call the method inc1(a).
inc1(a) method deals with a primitive. in this case the value of actual parameter is copied to the formal parameter of the method. whatever changes you did or do with this parameter inside the method are not affecting the actual primitive value. ok ?
but not in the case of inc2(bArr).....
here you are actually dealing with the reference to the object. and this object reference directly affects the object. ok ?
there is a story in campfire also called as pass by value and reference. hope you got the concept.
Originally posted by Rex Rodriguez:
It prints bArr[0] value as 1 but the value of a is printed as 0. It should
increment the value of a too, right? Can someone explain?
public class test {
public static void main(String args[]) {
int a=0; int b=0;
int[] bArr=new int[1]; bArr[0]=b;
inc1(a); inc2(bArr);
System.out.println("a="+a+" b="+b+" bArr[0]="+bArr[0]);
}
public static void inc1(int x) {x++; }
public static void inc2(int[] x) {x[0]++;}
}
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
mark,
OBJECTS ARE NOT PASSED BY REFERENCE IN JAVA!!!
A COPY OF the reference is passed. In Java everything is passed by value, primitive and references !!


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Rex Rodriguez:
It prints bArr[0] value as 1 but the value of a is printed as 0. It should
increment the value of a too, right? Can someone explain?
public class test {
public static void main(String args[]) {
int a=0; int b=0;
int[] bArr=new int[1]; bArr[0]=b;
inc1(a); inc2(bArr);
System.out.println("a="+a+" b="+b+" bArr[0]="+bArr[0]);
}
public static void inc1(int x) {x++; }
public static void inc2(int[] x) {x[0]++;}
}

The key is to remember that *all* variable parameters in java are passed by value. This means that when you call a method and pass it a variable, you are actually making a copy of the data in that variable and passing it to the method.
If the variable is a primitive, the value is just a copy of the variable's current value. If the variable is a reference type, the value is a copy of the reference in the variable.
For example, here's part of a method declaration:
public void foo(int x)
{
x = 12; //line 1
char c = 'a';
someMethodCall(c);//line 2
//
}

This method shows two examples. The int parameter x being passing in is a method variable; it is local in scope to the method. Now, some other method has called foo() with a value, but what was passed into foo was a copy of that value, so no matter *what* we do to x in method foo(), when foo() returns the caller's value is not changed.
int myVariable = 12;
foo(myVariable ); //think of this as foo(12);
System.out.println(myVariable);
myVariable will *ALWAYS* print 12, because method foo() cannot in any way change the value of the variable, since we only passed it a copy of the value.
Also, in line 2 above, when we call someMethodCall(c), we pass a copy of the value in variable c; so when this method returns, the value of c is unchanged.
Now, there is a slightly different twist when we pass in reference variables.
String myString = "hi there";
foo1(myString);
System.out.println(myString);
public void foo1(String localMyString)
{
localMyString= "goodbye."
}
What prints? "hi there" prints. Why? Because myString is a variable reference to a String object, but it is a variable still, and when we pass variables, we do so by making a copy of the information and passing it to the called method. Even though we change the local copy of the reference (we assign it to a different string object), the caller's value is not changed.
Now, it *is* possible to change data in an object via a reference, just like any object.
class Test { int i;}
Test aTest = new Test();
I can access my aTest's int member by saying
aTest.i = 5;
if I pass aTest to a method, I pass a copy of the value in aTest, which is a reference to the Test object I created above.
foo3(aTest);
System.out.println(aTest.i);
public void foo3(Test localTest)
{
localTest = new Test();
localTest.i = 10;
}
This prints 5 because the new Test object we created in foo3() was assigned to a local method arugment variable, and the original value in the caller's argument was not changed.
Finally,
Test aTest = new aTest();
aTest.i = 5;
foo4(aTest);
System.out.println(aTest.i);
public void foo4(Test localTest)
{
localTest.i = 333;
}
This prints 333! The caller's version of variable aTest still points to the original object that was created then given a value of 5 for i. When we call foo4(), we pass a copy of the value in the reference variable aTest. This value gets assigned to the local argument variable localTest. So far so good. Now, when we access the i class variable with
localTest.i
we are still accessing the original Test object, and thus changing it's value.
It's like when you write
String str = "my string";
String str2 = str;
System.out.println(str+str);
You get "my stringmystring" as output. You are referring to the same object with two different references. Likewise, when you pass a reference variable to a method, that method has access to the original object's accessible data members. It just can't make your reference variable "point" to a new object.
I hope this clears it up!
Rob
[ January 17, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
yes. you are right. but for remembering purposes i often call it that way so that i know the big difference between these two situations. For a starters it would be sort of ok to know that these two situations are different.
passing reference actually would mean passing a copy of that reference.

Originally posted by Valentin Crettaz:
mark,
OBJECTS ARE NOT PASSED BY REFERENCE IN JAVA!!!
A COPY OF the reference is passed. In Java everything is passed by value, primitive and references !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Value
 
Similar Threads
Array Question
can 2 objects of the same class acess private data of each other?
URGENT: Checking An Array For The Existance Of A Value
problem with passing arguments
Increment operator