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

pass by reference

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
A wop bop a lu bop a womp bam boom! Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic