Method with Parameters

In this chapter you will learn:

  1. How to add parameters to a method
  2. How to use class type as method parameters
  3. What is the difference between pass-by-value vs pass-by-reference

Parameterized method

A parameterized method can operate on a variety of data.

The new Rectangle class has a new method which accepts the dimensions of a rectangle and sets the dimensions with the passed-in value.

class Rectangle {
  double width;//from   j  av a2 s. c  o m
  double height;

  double area() {
    return width * height;
  }

  void setDim(double w, double h) { // Method with parameters
    width = w;
    height = h;
  }
}

public class Main {

  public static void main(String args[]) {
    Rectangle mybox1 = new Rectangle();
    double vol;
    mybox1.setDim(10, 20);

    vol = mybox1.area();
    System.out.println("Area is " + vol);

  }
}

The output:

Using Objects as Parameters

The following code passes objects to methods.

class Test {// ja v a2 s  .c o  m
  int a;

  Test(int i) {
    a = i;
  }
  boolean equals(Test o) {
    if (o.a == a )
      return true;
    else
      return false;
  }
}

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

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

  }
}

This program generates the following output:

Pass-by-value vs Pass-by-reference

When a parameter is passed into a method, it can be passed by value or by reference. Pass-by-value copies the value of an argument into the parameter. Changes made to the parameter have no effect on the argument. Pass-by-reference passes a reference to the parameter. Changes made to the parameter will affect the argument.

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

The following program uses the "pass by value".

class Test {/*from   j ava 2s  .c o  m*/
  void change(int i, int j) {
    i *= 2;
    j /= 2;
  }
}
public class Main {
  public static void main(String args[]) {
    Test ob = new Test();

    int a = 5, b = 20;

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

    ob.change(a, b);

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

The output from this program is shown here:

In the following program, objects are passed by reference.

class Test {/*from  ja v  a2 s  . c o  m*/
  int a, b;
  Test(int i, int j) {
    a = i;
    b = j;
  }
  void meth(Test o) {
    o.a *= 2;
    o.b /= 2;
  }
}
public class Main {
  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:

Next chapter...

What you will learn in the next chapter:

  1. How to overload methods with different parameters
  2. How does automatic type conversions apply to overloading