Hi friend,
When you call a method by reference, the callee sees the caller?s original variables passed as parameters, not copies. References to the callee?s objects are treated the same way. Thus any changes the callee makes to the caller?s variables affect the caller?s original variables. Java never uses call by reference. Java always uses call by value.
import java.io.*;
import java.awt.*;
public class callByReference {
public static void main(String[] args) {
System.out.println("This is call by reference example");
Rectangle rec = new Rectangle(20,30,200,50);
System.out.println("Before move up massage");
// System.out.println("r1 is now\n" + r1);
System.out.println("After move up massage display");
moveUp(rec);
System.out.println("r1 is now\n" + rec);
}
static void moveUp(Rectangle rect) {
// move up ten units
rect.translate(0,-20);
}
}
--------------------------------------------
public class CallByValue {
public static void main(String[] args) {
int i= 10;
double d= 50.0;
System.out.println("This is call by value example");
System.out.println("Massage 1: i= " + i + ", d= " + d);
Double(i, d); // No return value
System.out.println("Massage 2: i= " + i + ", d= " + d);
Double(i, i); //Java converts int to double
System.out.println("Massage 3: i= " + i);
}
public static void Double(int ii, double dd){
System.out.println("Triple 1: ii= " + ii + ", dd= " + dd);
ii *= 3; // ii= ii*3;
dd *= 3.0;
System.out.println("Triple 2: ii= " + ii + ", dd= " + dd);
}
}
----------------------------------------------------------
Read for more information.
http://www.roseindia.net/java/