jeffheaton's picture
in
Get the entire book!
Java for the Beginning Programmer

If you have worked with other programming languages you have likely heard of passing variables “by reference” or “by value”. If you are not familiar with these terms, the meaning refers to what happens to the value of a method or function’s arguments when that function ends. Consider Listing 6.7.

Listing 6.7: Passing by Value (MyClassArgument.java)

public class MyClassArgument
{
  public static void myMethod(int i)
  {
    i++;
  }

  public static void main(String args[])
  {
    int i = 10;
    myMethod(i);
    System.out.println("The value of i is " + i );
  }

}

What value would be printed out? The variable “i” was passed to myMethod and then increased by one. Would the program print out 10 or 11? That depends on if “i” was passed by reference or by value. Table 6.3 summarizes the differences between by reference and by value.

Table 6.3: The Difference Between by Reference and by Value

by reference Any changes made to the argument inside of the method or function are reflected outside the method or function as well.
by value A copy of the variable is made for the method, so any changes made to the argument inside of the method or function are not kept when the method or function returns.

Usually variables are passed by value in Java. When a non-object variable, such as an int, is passed to a method or function it is by value. So in the above example, myMethod would have no effect on the variable 1. The value 10 would be printed. Java does not allow you to choose when a variable is passed by reference or by value. It is governed by a set of rules. These rules are shown in Table 6.4.

Table 6.4: Is it by Value or by Reference

Type By Value or by Reference
Primitive data types (i.e. ints) by value
Object References by value
Objects by reference
Arrays by reference

Some of these terms may not be familiar to you. Arrays will be covered in Chapter 6. Primitive data types are all of the built in non-object types supported by Java. Java supports eight primitive data types: byte, short, int, long, char, float, double and boolean.

It is important to understand the difference between an object and an object reference. An object is the actual memory image of the object. You can not directly access an object in Java, you must access it through object references. Objects in Java are always passed by reference, but their object references are always passed by value. To see the difference consider Listing 6.8.

Listing 6.8: By Value and By Reference (MethodCall.java)

import javax.swing.*;

public class MethodCall
{

  static void changeValue(JButton button)
  {
// Change the text of the button, this 
// new value is reflected outside of the
// call to "changeValue"
    button.setText("New value");
  }

  static void changeReference(JButton button)
  {
// Create a new button, and assign its 
// reference to "button". This change is
// not reflected outside of the call 
// to "changeValue"
    button = new JButton("New value");
  }

  static void changePrimitive(int i)
  {
    i = i + 1;
  }

  /**
   * Main entry point for example.
   * @param args Not used.
   */
  public static void main(String args[])
  {
    // setup the variables
    JButton button1 = new JButton("Old Value");
    JButton button2 = new JButton("Old Value");
    int var = 5;

    // call the methods
    changeValue(button1);
    changeReference(button2);
    changePrimitive(var);

    // display the new values
    System.out.println("Button1:" + 
      button1.getText());
    System.out.println("Button2:" + 
      button2.getText());
    System.out.println("Primitive variable:" + var);
  }
}

In this example two object references are created button1 and button2. These references both point to two separate JButton objects. We will see more about JButton in Chapter 10. For now we only care about a text string that it holds. To change this text string you have to call JButton’s setText method. If you want to get the text string back, you have to call JButton’s getText method. This will allow you to see the difference between a modification to an object reference and a modification to the object itself.

When the button parameter is passed to “changeReference” and “changeValue”, the button is passed by reference. Buttons are always passed by reference, because they are objects. However, inside of each method, you have the opportunity to change the button. The method changeValue changes the actual object, as it calls the setText method to change the object. Changes made by changeValue will be reflected outside of changeValue. The method changeReference will only change the reference, and its changes will not be reflected outside of the changeReference method.


Copyright 2005 - 2012 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright, license and trademark information.