Java. Methods. Passing parameters into methods (functions) of the class. Passing of variables of primitive data types and objects into a method as a parameter




Methods. Passing parameters into methods (functions) of the class. Passing of variables of primitive data types and objects into a method as a parameter


Contents


Search other websites:

1. What are the ways to pass arguments into the method (function)?

There are 2 ways of passing variable or passing a class object into a function:

  • passing by value. In this case, the value of the argument is copied to the formal parameter of the function. Since a copy of the argument is created in the function, all changes over the copy will not affect the value of the argument;
  • passing by reference. In this case, a reference to the argument that is used for the call is passed to parameter. Using this reference has access to the argument. Thus, all changes made in the body of the function over the value of the parameter will change the value of the argument that was passed to the function.

2. What are the features of passing the argument of a primitive type?

If a primitive type argument is passed to the method, then a passing by value occurs. That is, a copy of the argument is made.

3. How is an object of a certain class passed to the method?

Unlike variables of primitive types, class objects are passed by reference. This means that the changes made in the body of the function (method) will also change the values of the object that was specified as an argument.



4. Examples of passing the variables of primitive types to methods (functions)

Example 1. Implementation of Max() method, which accepts two parameters of primitive data type.

static int Max(int a, int b)
{
    if (a>b) return a;
    else return b;
}

Using the method in another program code:

public static void main(String[] args)
{
    int t, x, y;
    x = 133;
    y = 68;
    t = Max(x, y); // t = 133
    System.out.println("Max = " + t);
}

Example 2. Implementing the Inc()method to increment (decrement) the integer varibale ‘value’ by the value of the input argument ‘step’. In the body of the method, the value of the ‘value’ parameter is changed. However, after the method is completed, the value of the x argument in the main() function remains unchanged. This means that the parameter was passed by value.

The implementation of method is as follows:

static int Inc(int value, int step)
{
    value += step;
    return value;
}

Using the Inc() method in function main():

public static void main(String[] args)
{
    int t, x;
    x = 13;
    t = Inc(x, 5); // t = 13+5 = 18; x = 13 - without changes
    System.out.println("t = " + t);
    System.out.println("x = " + x);
}

5. Example of passing a class object to a method (function)

In the example, the Month class that defines the month of the year is implemented. In the class are declared:

  • internal variable month;
  • three constructors;
  • three access methods.

A class object is passed to one of the class constructors for initial initialization. Also, the object of the Month class is passed to one of the SetMonth() methods.

The code that implements the Month class looks like this:

public class Month
{
    private int month;

    Month() { month = 1; }
    Month(int nmonth) { month = nmonth; }
    Month(Month mn) { month = mn.month;       }

    // методы доступа
    int GetMonth() { return month; }
    void SetMonth(int month) { this.month = month; }
    void SetMonth(Month mn) { month = mn.month; }

    public static void main(String[] args)
    {
        Month m1 = new Month(); // constructor without parameters
        Month m2 = new Month(5); // constructor with 1 parameter of int type
        Month m3 = new Month(m2); // constructor with 1 parameter, which is the object of Month class
        int t;

        t = m1.GetMonth(); // t = 1
        t = m2.GetMonth(); // t = 5
        t = m3.GetMonth(); // t = 5

        // passing a class object to SetMonth() method
        m1.SetMonth(m2);
        t = m1.GetMonth(); // t = 5

        m2.SetMonth(8);
        m1.SetMonth(m2);
        t = m1.GetMonth(); // t = 8
        System.out.println("Month = " + t);
    }
}

6. How the returning of a class object from a method is performed by the ‘return’ operator? Example

In addition to primitive data types, methods (functions) can return the class objects. In this case, the memory is allocated for the class object in the body of method. After use, this object is returned from the method using the ‘return’ statement.

In the calling part of the program, you do not need to allocate memory for the object to which the return value from the function is assigned. This is because the memory for all objects in the program is called dynamically. While there is a reference to the object, it is not deleted from memory. So, when assigning like:

varObj = Method(...);

there will be a reference to the memory allocated dynamically within the Method() method. Thus, for the varObj variable, you do not need to allocate memory in the calling code.

Example. Let there be given a class MyDay, which implements the day of week.

public class MyDay
{
    private int day; // internal variable day = 1..7

    // access methods
    public int GetDay() { return day; }
    public void SetDay(int nday) { day = nday; }
}

public class CDay
{
    // methods, which returns the object of MyDay class
    public static MyDay GetObjDay(int day)
    {
        MyDay tmpObj = new MyDay(); // memory is allocated dynamically
        tmpObj.SetDay(day);
        return tmpObj; // returning from method: in the calling method you do not need to allocate the memory
    }

    public static void main(String[] args)
    {
        MyDay d = new MyDay(); // the object of MyDay class - memory is allocated
        MyDay d2; // memory does not need to be allocated

        d2 = GetObjDay(5); // d2 refers to the memory that is allocated in the GetObjDay() method
        int t = d2.GetDay(); // t = 5
        System.out.println("Day = " + t);
    }
}


Related topics