Java passes all arguments by value, not by reference. However this is one of the few places where the distinction between an object and a reference to an object becomes important. Object and array variables in Java are really references to the object or array. This can make it look like an object is passed by reference if you only modify the fields of the object or array, but do not change the reference itself. For example, consider this program:

import java.awt.Point;

class changePoint {

  public static void main(String args[]) {
  
    Point p1 = new Point(0, 0);
    changePoint(p1);
    System.out.println(p1);
    
  }
  
  static void changePoint(Point p) {
  
    p.x = 38;
    p.y = 97;

  }

}

It prints:

java.awt.Point[x=38,y=97]

Therefore the point has been changed. However the reference, which is what was really passed, has not been changed. To see that consider the following program.

 

import java.awt.Point;


class dontChangePoint {

  public static void main(String args[]) {
  
    Point p1 = new Point(0, 0);
    dontChangePoint(p1);
    System.out.println(p1);
    
  }
  
  static void dontChangePoint(Point p) {
  
    p = new Point(38, 97);

  }

}

It prints:


java.awt.Point[x=0,y=0]

What happened in this example was that a copy of the reference p1 was passed to the dontChangePoint() method. A new Point object was then assigned to that copy. However this did not change the old reference in the main method. In the previous example the reference p in the changePoint() method and p1 in the

Java passes all arguments by value, not by reference. However this is one of the few places where the distinction between an object and a reference to an object becomes important. Object and array variables in Java are really references to the object or array. This can make it look like an object is passed by reference if you only modify the fields of the object or array, but do not change the reference itself. For example, consider this program:

import java.awt.Point;

class changePoint {

  public static void main(String args[]) {
  
    Point p1 = new Point(0, 0);
    changePoint(p1);
    System.out.println(p1);
    
  }
  
  static void changePoint(Point p) {
  
    p.x = 38;
    p.y = 97;

  }

}

It prints:

java.awt.Point[x=38,y=97]

Therefore the point has been changed. However the reference, which is what was really passed, has not been changed. To see that consider the following program.

 

import java.awt.Point;


class dontChangePoint {

  public static void main(String args[]) {
  
    Point p1 = new Point(0, 0);
    dontChangePoint(p1);
    System.out.println(p1);
    
  }
  
  static void dontChangePoint(Point p) {
  
    p = new Point(38, 97);

  }

}

It prints:


java.awt.Point[x=0,y=0]

What happened in this example was that a copy of the reference p1 was passed to the dontChangePoint() method. A new Point object was then assigned to that copy. However this did not change the old reference in the main method. In the previous example the reference p in the changePoint() method and p1 in the main() method both referred to the same object. In this example p and p1 refer to different objects after the new Point is assigned to p.

Posted in: Java

Related FAQ's

Marius Ion ANGEL HOT SOFT LLC (800) 316-7677