Argument Passing


x

Lesson:
Main Points:
  1. When primitives are passed as arguments to a method, only copies of the original values are manipulated within the method.
  2. The JVM treats objects differently from primitives.
  3. Objects are passed by reference.
  4. Arrays are objects.


EXAMPLE:

import java.awt.*; import java.applet.*; public class ex07 extends Applet{ Dimension d; public void init(){ setBackground(Color.white); d=getSize(); } public void prim(int i){ i+=10; } public void arrI(int[] x){ x[0]+=10; } public void dir(myInt x){ x = new myInt(22); } public void ind(myInt x){ x.add(10); } public void paint(Graphics g){ int i=9; // primitive int a[] = { 9 }; // array containing primitive values myInt m1 = new myInt(9); myInt m2 = new myInt(9); g.drawRect(0,0,d.width-1,d.height-1); g.setFont(new Font("Courier", Font.BOLD, 14)); g.drawString("Primitives vs. Objects", 10, 20); g.drawLine(0,28,d.width,28); g.drawLine(200,28,200,d.height); g.setFont(new Font("Courier", Font.PLAIN, 10)); g.drawString("Before being passed to method:", 10, 50); g.drawString("Primitive value: "+i, 10, 65); g.drawString("Primitive in array: " + a[0], 10, 80); g.drawString("Object direct: "+m1.getValue(), 10, 95); g.drawString("Object indirect: "+m2.getValue(),10,110); prim(i); arrI(a); dir(m1); ind(m2); g.drawString("After being passed to method:", 210, 50); g.drawString("Primitive value: "+i, 230,65); g.drawString("Primitive in array: "+a[0], 230, 80); g.drawString("Object direct: "+m1.getValue(), 230,95); g.drawString("Object indirect: "+m2.getValue(), 230, 110); } } class myInt{ int val; myInt(int x){ val = x; } public int getValue(){ return val; } public void add(int x){ val+=x; } }
This example covers four possible cases:
  1. PRIMITIVE: In the paint method a variable of type int called i is declared and initialized to the value 9. After i is printed in the BEFORE column it is passed to prim which receives an int value and increments this value. Since with primitives only a copy of the variable is sent to the method, i remains unchanged as is seen when it is printed out in the AFTER column.
  2. PRIMITIVES STORED IN ARRAYS: Since arrays are objects, they behave a little differenently. In the paint method an array of type int called a is declared, constructed, and initialized to contain only one element with a value of 9. The entire array is passed as an argument to the method arrI which receives the entire array (not just a single int value). arrI acts on an element in an array (as opposed to a single integer, this is an important distinction to make). As a result the value stored in a[0] is changed when it is printed out in the AFTER column.
  3. NEW OBJECTS: The instance of myInt called m1 is initialized to 9 and prints out this way in the BEFORE column. When it gets passed to the method called dir (which receives an object of type myInt), dir creates a new object which is stored as x. Within dir x has a new identity, but m1 is not affected by this since it was passed by reference.
  4. ALTERED OBJECTS: The instance of myInt called m2 is initialized to 9 and prints out this way in the BEFORE column. When it gets passed to the method called ind (which receives an object of type myInt), ind calls a method defined in myInt called add which alters the value stored as x within any instance of myInt. Since m2 is still the same object, and the value stored within this object was altered through use of a class method, the value is changed as seen in the AFTER column.

These four test cases reveal a lot about how the Java Virtual Machine (JVM) works. You should study this information carefully.

The example does one thing which may complicate things a bit if you are not familiar with classes (something which will be discussed later in this course). The class myInt is defined in the same file as the ex07 example. It was created to make it possible to illustrate how class references work.


x

Assignment:
You will create a simple applet which is arranged like the one above, but you will have only three test cases (one involving a primitive and two involving arrays).

  1. PRIMITIVES: Same as example.
  2. ARRAYS EXAMPLE ONE: Same as example.
  3. ARRAYS EXAMPLE TWO: Instead of passing the entire array, pass just a[0]. Before you do the test try to predict what will happen. To do this you will have to create another method (call it arrVal for array value) which will take one int value as an argument. HINT: Except for the name it will be identical to the method you used for primitives.

x