• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Passing objects by reference or value???

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
------------------
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also how is this different form what we do in C++??
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So why will the above code not work?? Please elaborate..
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its because u cannot pass primitive variables by reference
 
hmehta
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool got it!!!
Thanks Stephanie for your time and effort!!!
 
today's feeble attempt to support the empire
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic