aspose file tools*
The moose likes Java in General and the fly likes Passing objects by reference or value??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing objects by reference or value???" Watch "Passing objects by reference or value???" New topic
Author

Passing objects by reference or value???

hmehta
Greenhorn

Joined: Aug 30, 2000
Posts: 20
Hello all,
I am quite confused by this topic. The book on core java by Sun microsystems says that pobjects in java are always passed by value while Bruc eckels book sremains non commital on this and tends to point that actually object are apassed by reference. I also feel that it should be by reference. can anyone explain this with some clear examples and help clear the confusion...
Thanks..
Vim Win
Greenhorn

Joined: May 01, 2004
Posts: 21
You have an interesting article to read at the following address
http://www.javaranch.com/campfire/StoryCups.html
I hope this would clear your confusion.
JVRN.
------------------
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
hmehta,
Java programs do not deal directly with objects, but rather with object references. You can think of an object reference as the unique identifier of an object.
Like Core Java says, objects in java are always passed by VALUE.
This means that when you pass an object as an argument to a method, you actually pass a COPY of that object's reference. If you try to change this reference inside your function, the changes will be lost as soon as your function returns. This is because you really only changed the COPY of the original reference.
However, if you use the copy of the object reference to make changes to the object's members inside your function, these changes will exist after your function returns. Below is a small program to illustrate these points:
class Test
{
StringBuffer theBuffer;
Test()
{
theBuffer = new StringBuffer("original string");
}
void reassignBuffer(StringBuffer s)
{
s = new StringBuffer("the buffer from reassignBuffer");
}
void changeObjectViaRefValue(StringBuffer s)
{
s.append(" plus some stuff");
}
public static void main(String args[])
{
Test myTest = new Test();
System.out.println("Before any methods called, theBuffer = " +
myTest.theBuffer);
myTest.reassignBuffer(myTest.theBuffer);
System.out.println("After call to reassignBuffer, theBuffer = " +
myTest.theBuffer);
myTest.changeObjectViaRefValue(myTest.theBuffer);
System.out.println("After call to changeObjectViaRefValue, theBuffer = " +
myTest.theBuffer);
}
}
When we start, theBuffer = "original string";
We call reassignBuffer() with a COPY of this object's reference. Inside reassignBuffer(), we try to make some changes to the argument. But because we are doing this to the copy, the changes don't stick after the method returns.
Next we call changeObjectViaRefValue() with a COPY of this object's reference. However, rather than trying to change the reference that we got as an argument, we make changes to the object that this reference points to. For this reason, the changes are still in effect after the method returns.
I hope this helps clear things up.
Stephanie
hmehta
Greenhorn

Joined: Aug 30, 2000
Posts: 20
thanks this site was great and it cleared a lot of things...
However I have another question. Say I want to swap two int
values and also if I want to swap two object , willl the following work
swap(int x, int y)
{
int temp=x;
x=y;
y=temp;
}
swap(Cat a, Cat b)
{
cat x=new cat x();
cat x=a;
a=b;
b= cat x;
}
Will the above work???
hmehta
Greenhorn

Joined: Aug 30, 2000
Posts: 20
Also how is this different form what we do in C++??
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
hmehta,
The code will NOT work to swap the values as it is written. To change values within a function, use single-element arrays. See example below:
class Test
{
void swap(int x, int y) // this won't work
{
int temp = x;
x = y;
y = temp;
}
void swap(int x[], int y[]) // this will work
{
int temp = x[0];
x[0] = y[0];
y[0] = temp;
}
public static void main(String args[])
{
Test myTest = new Test();
int a = 5;
int b = 10;
System.out.println("before swap, a = " + a +
", b = " + b);
myTest.swap(a, b);
System.out.println("after swap, a = " + a +
", b = " + b);
int arrayA[] = new int[1];
arrayA[0] = a;
int arrayB[] = new int[1];
arrayB[0] = b;
System.out.println("before swap, arrayA[0] = " + arrayA[0] +
", arrayB[0] = " + arrayB[0]);
myTest.swap(arrayA, arrayB);
System.out.println("before swap, arrayA[0] = " + arrayA[0] +
", arrayB[0] = " + arrayB[0]);
}
}
Hope this helps.
Stephanie
hmehta
Greenhorn

Joined: Aug 30, 2000
Posts: 20
So why will the above code not work?? Please elaborate..
Sunita Vontel
Ranch Hand

Joined: Aug 28, 2000
Posts: 72
I think its because u cannot pass primitive variables by reference
hmehta
Greenhorn

Joined: Aug 30, 2000
Posts: 20
I van undertsnd that Swap(Object a, Object b)
will not work as they are object s but
Swap(int x, int y) Why will this not work???
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
hmehta,
In Java, arguments get passed to methods by VALUE, not by reference. This includes primitive types like int, as well as objects.
You sound like you know some C++, so I will try to explain it from that perspective. Look at the C++ code below:
#include "stdafx.h"
#include "iostream.h"
// passing by value
void swap1(int a, int b) { int temp = a; a = b; b = temp;}
// passing by reference
void swap2(int &a, int &b) { int temp = a; a = b; b = temp;}
int main(int argc, char* argv[])
{
int x = 5;
int y = 10;
cout << "before first swap, x = "
<< x << ", y = " << y << endl;
swap1(x, y);
cout << "after first swap, x = "
<< x << ", y = " << y << endl;
cout << "before second swap, x = "
<< x << ", y = " << y << endl;
swap2(x, y);
cout << "after second swap, x = "
<< x << ", y = " << y << endl;
return 0;
}
swap1() passes by value, so the values of x and y do NOT change. swap2() passes by reference, so the values of x and y DO change.
In Java, this is not an option. It is ALL pass-by-value. If you want your method to ACT like pass-by-reference, you need to use the trick with the one-element arrays from the earlier post.
Does this make more sense to you?
Stephanie
hmehta
Greenhorn

Joined: Aug 30, 2000
Posts: 20
Cool got it!!!
Thanks Stephanie for your time and effort!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing objects by reference or value???
 
Similar Threads
please explain
hibernate mapping question
[immutable]...
Amazing Framework
Call by Reference and Call by Value Question