Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click NOW to join Planet IT!
Click NOW to join Planet IT!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Passing arguments to methods

There are two ways to pass an argument to a method: by value and by reference. The difference is in what happens to the variable passed in the calling method as a result of what’s done to it in the called method. For example, consider this code fragment:

     int a = 7;
     changeVariable(a);
     System.out.println(a);

Now suppose the changeVariable() method looks like this:

     public void changeVariable(int a) {
      a = 10;
     }

What value gets printed — 7 or 10? If the argument a is passed by value, then a copy of variable a’s value is used by the changeVariable() method. The changeVariable() method never gets access to the original variable a in the calling method. It has a different variable, also named a. Therefore, the calling method prints the value 7.

On the other hand, if a is passed by reference, then the changeVariable() method does not get a copy of the variable named a. It gets the real thing. The name a in the calling method and the name a in the changeVariable() method refer to the same variable. Therefore, System.out.println() prints 10.

Note that the names aren’t important here. If changeVariable() were written using i or some name other than a for its argument, the result would be the same. What makes the difference is whether the variable is passed by reference or by value. If a variable is passed by reference, it can change in the calling method. If it’s passed by value, it cannot. Java passes all arguments by value, not by reference.

Because object and array variables in Java are references to the object or array, it can appear as if an object is passed by reference if you modify only 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;

      }

     }

This program prints

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

The point has therefore 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 the following:

     java.awt.Point[x=0,y=0]
Once and for all: Does Java Have Pointers?

The issue of whether Java really has pointers seems to generate countless flame wars on Usenet, almost as many as are generated by Star Trek trivia. Some of this confusion is a result of incorrect or incomplete knowledge. Even more of the confusion is a result of using the same word to mean two different things.

In this book, I use the word pointer to mean the address of a byte of memory on the computer. This is the definition of pointer used by assembly language, C, and C++ programmers. Java has no equivalent for this kind of pointer.

Some programmers, particularly those accustomed to pointerless languages like Fortran 77 and Basic, use the word pointer in a very abstract sense, in which it is just about anything that gives a reference to or points to a block of data. Thus, an array that contains indexes of entries in another array is often said to be an array of pointers, although C programmers would not recognize it as such. In this sense, Java does have pointers.

Some programmers also claim that Java has pointers because the virtual machine may use pointers in the same sense as used by assembly language programmers to implement Java’s references. In fact, some implementations of Java, particularly Microsoft’s, do use pointers in exactly this fashion. However, the .class file verifier severely restricts what you can do with these pointers. In particular, you cannot use them as freely as you can in C or assembly language. You cannot perform arithmetic on them. You cannot convert them to and from numeric data types like int. Furthermore, the virtual machine need not implement references as pointers. Sun’s virtual machines use handles instead. Others may use something completely different, like numeric indexes into a large, static array.

The bottom line is that Java doesn’t have pointers in the sense that 95 percent of the people who talk about pointers mean. It’s best just to use the term reference, and try to stay out of flame wars when possible.

In this example, a copy of the reference p1 was passed to the dontChangePoint() method. A new Point object was then assigned to that copy. This, however, 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.

Special references

There are three special references in Java source code: null, this, and super. The meaning of these references generally depends on their context.

The null reference

The null reference is an invalid reference. It has no type, and thus may be assigned to a variable of any reference type. When a reference variable is declared but not constructed, it is initially equal to null.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.