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

pass by reference

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi,
what is pass by reference? what is difference between pass by reference and pass by value in java?Does java pass by reference? Can anybody explain me with an example?
Thanks a lot in advance,
angela
Sri Bala
Ranch Hand

Joined: Mar 06, 2001
Posts: 63
All basic types are passed by value. Arrays and Objects are passed by reference. See example below.
The first swap didn't work because int is passed by value.
The second swap works because arrays are passed by reference.
The third won't work because though you pass the Integer arrays by reference, the actual object is not changed inside and only the temporary references are changed. To change the actual object, you will have to use set members. I couldn't show that here because Integer doesn't have something like setValue(). Assuming there's a setValue() method for Integer, The code would look like
int k = i.getInteger();
i.setValue( j.getInteger() );
j.setValue( k );
Swap.java
---------
public class Swap {
public void doSwap( int i, int j )
{
int k=i;
i=j;
j=k;
return;
}
public void doSwap( Integer i, Integer j )
{
Integer k = i;
i = j;
j = k;
return;
}
public void doSwap( int [] i, int [] j )
{
int k = i[0];
i[0] = j[0];
j[0] = k;
return;
}
public static void main(String [] args)
{
int a=10, b=20;
Swap swap = new Swap();
swap.doSwap( a, b );
System.out.println(" a, b = " + a + ", " +b);
int [] a1 = { 10 };
int [] b1 = { 20 };
swap.doSwap( a1, b1 );
System.out.println(" a1, b1 = " + a1[0] + ", " +b1[0]);

Integer x = new Integer(10);
Integer y = new Integer(20);
swap.doSwap( x, y );
System.out.println(" x, y = " + x + ", " +y);
}
}
[This message has been edited by Sri Bala (edited March 07, 2001).]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Bala,
Everything in Java is passed by value. No exceptions.
It is true that you can alter an object through a reference, but the formal parameter still only receives a copy of the actual parameter's value. If you change the formal parameter inside your method, the actual parameter will not be affected.
For example,
<pre>
int[] z = {6, 7, 8};
void foo() {
int[] x = {1,2,3};
bar(x); // x is the actual parameter
// x is now {5,2,3} not {6,7,8}
}

void bar(int[] a) { // a is the formal parameter
a[0] = 5;
a = z; // if passed by reference, this should also point x to z
}
</pre>

J.Lacar

Originally posted by Sri Bala:
All basic types are passed by value. Arrays and Objects are passed by reference. See example below.


Junilu - [How to Ask Questions] [How to Answer Questions]
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
Originally posted by JUNILU LACAR:
Everything in Java is passed by value. No exceptions.

That means can we say that -
an Object reference in java is passed by value to a function, that means whatever you assign to the formal parameter in the function doesnot reflect outside of the function. rite?
A simple example of this is in Thinking in java by BruceEckel
class Letter {
char c;
}
public class PassObject {
static void f(Letter y) {
y.c = 'z';
}
public static void main(String[] args) {
Letter x = new Letter();
x.c = 'a';
System.out.println("1: x.c: " + x.c);
f(x);
System.out.println("2: x.c: " + x.c);
}
}
The output will be
1: x.c: a
2: x.c: z
I think its basically aliasing.
Cheers
Siva
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass by reference