Wednesday 8 March 2017

Using Objects as Parameters, A Closer Look at Argument Passing & Returning Objects - Java Tutorials

Using Objects as Parameters


So far we have only been using simple types as parameters to methods. However, it is both correct and common to pass objects to methods. For example, consider the following short program:

  // Objects may be passed to methods.
  class Test {
    int a, b;

    Test(int i, int j) {
      a = i;
      b = j;
    }

    // return true if o is equal to the invoking object
    boolean equals(Test o) {
      if(o.a == a && o.b == b) return true;
      else return false;
    }
  }

  class PassOb {
    public static void main(String args[]) {
      Test ob1 = new Test(100, 22);
      Test ob2 = new Test(100, 22);
      Test ob3 = new Test(-1, -1);

      System.out.println("ob1 == ob2: " + ob1.equals(ob2));

      System.out.println("ob1 == ob3: " + ob1.equals(ob3));
    }
  }

This program generates the following output:

  ob1 == ob2: true
  ob1 == ob3: false

As you can see, the equals( ) method inside Test compares two objects for equality and returns the result. That is, it compares the invoking object with the one that it is passed. If they contain the same values, then the method returns true. Otherwise, it returns false. Notice that the parameter o in equals( ) specifies Test as its type. Although Test is a class type created by the program, it is used in just the same way as Java’s built-in types.

One of the most common uses of object parameters involves constructors. Frequently you will want to construct a new object so that it is initially the same as some existing object. To do this, you must define a constructor that takes an object of its class as a parameter. For example, the following version of Box allows one object to initialize another:

  // Here, Box allows one object to initialize another.

  class Box {
    double width;
    double height;
    double depth;

    // construct clone of an object
    Box(Box ob) { // pass object to constructor
      width = ob.width;
      height = ob.height;
      depth = ob.depth;
    }

    // constructor used when all dimensions specified
    Box(double w, double h, double d) {
      width = w;
      height = h;
      depth = d;
    }

    // constructor used when no dimensions specified
    Box() {
      width = -1; // use -1 to indicate
      height = -1; // an uninitialized
      depth = -1; // box
    }

    // constructor used when cube is created
    Box(double len) {
      width = height = depth = len;
    }

    // compute and return volume
    double volume() {
      return width * height * depth;
    }
  }

  class OverloadCons2 {
    public static void main(String args[]) {
      // create boxes using the various constructors
      Box mybox1 = new Box(10, 20, 15);
      Box mybox2 = new Box();
      Box mycube = new Box(7);

      Box myclone = new Box(mybox1);

      double vol;

      // get volume of first box
      vol = mybox1.volume();
      System.out.println("Volume of mybox1 is " + vol);

      // get volume of second box
      vol = mybox2.volume();
      System.out.println("Volume of mybox2 is " + vol);

      // get volume of cube
      vol = mycube.volume();
      System.out.println("Volume of cube is " + vol);

      // get volume of clone
      vol = myclone.volume();
      System.out.println("Volume of clone is " + vol);
    }
  }

As you will see when you begin to create your own classes, providing many forms of constructor methods is usually required to allow objects to be constructed in a convenient and efficient manner.





A Closer Look at Argument Passing

In general, there are two ways that a computer language can pass an argument to a subroutine. The first way is call-by-value. This method copies the value of an argument into the formal parameter of the subroutine. Therefore, changes made to the parameter of the subroutine have no effect on the argument. The second way an argument can be passed is call-by-reference. In this method, a reference to an argument (not the value of the argument) is passed to the parameter. Inside the subroutine, this reference is used to access the actual argument specified in the call. This means that changes made to the parameter will affect the argument used to call the subroutine. As you will see, Java uses both approaches, depending upon what is passed.

In Java, when you pass a simple type to a method, it is passed by value. Thus, what occurs to the parameter that receives the argument has no effect outside the method. For example, consider the following program:

  // Simple types are passed by value.
  class Test {
    void meth(int i, int j) {
      i *= 2;
      j /= 2;
    }
  }

  class CallByValue {
    public static void main(String args[]) {
      Test ob = new Test();

      int a = 15, b = 20;

      System.out.println("a and b before call: " + a + " " + b);

      ob.meth(a, b);

      System.out.println("a and b after call: " + a + " " + b);
    }
  }

The output from this program is shown here:

  a and b before call: 15 20
  a and b after call: 15 20

As you can see, the operations that occur inside meth( ) have no effect on the values of a and b used in the call; their values here did not change to 30 and 10.

When you pass an object to a method, the situation changes dramatically, because objects are passed by reference. Keep in mind that when you create a variable of a class type, you are only creating a reference to an object. Thus, when you pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument. This effectively means that objects are passed to methods by use of call-by-reference. Changes to the object inside the method do affect the object used as an argument. For example, consider the following program:

  // Objects are passed by reference.
  class Test {
    int a, b;

    Test(int i, int j) {
      a = i;
      b = j;
    }

    // pass an object
    void meth(Test o) {
      o.a *= 2;

      o.b /= 2;
    }
  }

  class CallByRef {
    public static void main(String args[]) {
      Test ob = new Test(15, 20);

      System.out.println("ob.a and ob.b before call: " +
                                       ob.a + " " + ob.b);

      ob.meth(ob);

      System.out.println("ob.a and ob.b after call: " +
                                      ob.a + " " + ob.b);
    }
  }

This program generates the following output:

  ob.a and ob.b before call: 15 20
  ob.a and ob.b after call: 30 10

As you can see, in this case, the actions inside meth( ) have affected the object used as an argument.

As a point of interest, when an object reference is passed to a method, the reference itself is passed by use of call-by-value. However, since the value being passed refers to an object, the copy of that value will still refer to the same object that its corresponding argument does.

When a simple type is passed to a method, it is done by use of call-by-value. Objects are passed by use of call-by-reference.




Returning Objects

A method can return any type of data, including class types that you create. For example, in the following program, the incrByTen( ) method returns an object in which the value of a is ten greater than it is in the invoking object.

  // Returning an object.
  class Test {
    int a;

    Test(int i) {
      a = i;
    }

    Test incrByTen() {
      Test temp = new Test(a+10);
      return temp;
    }
  }

  class RetOb {
    public static void main(String args[]) {
      Test ob1 = new Test(2);
      Test ob2;

      ob2 = ob1.incrByTen();
      System.out.println("ob1.a: " + ob1.a);
      System.out.println("ob2.a: " + ob2.a);

      ob2 = ob2.incrByTen();
      System.out.println("ob2.a after second increase: " + ob2.a);
    }
  }

The output generated by this program is shown here:

  ob1.a: 2
  ob2.a: 12
  ob2.a after second increase: 22

As you can see, each time incrByTen( ) is invoked, a new object is created, and a reference to it is returned to the calling routine.

The preceding program makes another important point: Since all objects are dynamically allocated using new, you don’t need to worry about an object going out-of-scope because the method in which it was created terminates. The object will continue to exist as long as there is a reference to it somewhere in your program. When there are no references to it, the object will be reclaimed the next time garbage collection takes place.

No comments:

Post a Comment