Home : Course Map : Chapter 3 : Java :
Demo 4: Value vs Reference
JavaTech
Course Map
Chapter 3

Introduction
Class Definition
Data Fields
Methods
Constructors
Instantiation
  
Demo 1
  Demo 2
Static Members
  Demo 3

Value&Reference
  Demo 4
Overloading
   Demo 5
Wrappers 
  Demo 6
Autobox/Unbox
   Demo 7
Arrays
  Demo 8
Exceptions
Exercises

    Supplements
MoreAboutObjects
Creating Types

Classes,Objs&JVM

Java OOP vs C++
Garbage Collection
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The example below looks at the passing of primitive values and references in method arguments.

Note: Both methods in the example below use the same name but different types of arguments. We will discuss such method overloading in the next section.

In main the invoking of the method aMethod(int j) in the class Test will cause a copy of the value in main to pass into the value of the j in the method. So even though the code in aMethod(int j)changes the value of the local j variable, the j back in main remains unaffected.

We used the same name j for both int local variables in main and aMethod. However, these are completely different variables, occupying different locations in memory. We could use different names if we wished.

We also invoke the method aMethod(String aStr). This will send a copy of the reference pointer in main to the method. With this reference the method can access the same string data as the reference in main. That is, they both point to the same String object.

The code in aMethod(String aStr)changes the value of the local aStr reference variable but this does not affect the aStr reference variable back in main().

ValRefApplet.java
(Output goes to browser's Java console.)

public class ValRefApplet extends java.applet.Applet
{
  public void init () {

    // Create an instance of the Test class
    Test test = new Test ();

    int j = 5;
    test.aMethod (j);
    System.out.println ("In main");
    System.out.println ("j = " + j);
    System.out.println ();

    String aStr = new String ("A string");
    test.aMethod (aStr);
    System.out.println ("In main");
    System.out.println ("aStr = " + aStr);
    System.out.println ();
  }

  // Paint message in Applet window.
  public void paint (java.awt.Graphics g) {
    g.drawString ("ValRefApplet", 20, 20);
  }
}

/**
  * Test has one constructor with a primitive type argument
  * and one with a object (String) reference argument.
**/
class Test
{
  void aMethod (int j) {
    System.out.println (" In aMethod (int j)");
    System.out.println ("  j = " + j);
    j = 10;
    System.out.println ("  j = " + j);
    System.out.println ();
  }

  void aMethod (String aStr) {
    System.out.println (" In aMethod (String aStr)");
    System.out.println ("  aStr = " + aStr);
    aStr = new String ("different string");
    System.out.println ("  aStr = " + aStr);
    System.out.println ();
  }
}

Application version:
ValRefApp.java

 

Latest update: Oct. 19, 2004

 

           Tech
OOP in Tech Apps
Complex Number
Histogram Class
  Demo
More Wrappers

           Physics
OOP in Physics
Particle Class
Root Finding
  Demo 1
Newton Methods
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.