Menu
Topics Index
...
`


Methods Overiding, Overloading >
Siva Nookala - 07 Apr 2016
For any method both primitive data types and non-primitive data types can be passed as parameters. i.e. primitive data types like int, char, double can be passed as well as non-primitive data types like Student, Book, Bike can be passed as parameters. Passing primitive data types is usually referred as Pass By Value, where as passing non-primitive data types is Pass By Reference.

Pass By Value
class PassByValue
{
    public static void main(String s[])
    {
        int i = 5;
        System.out.println("i before increment : " + i);
        increment(i);
        System.out.println("i after increment : " + i);
    }
    
    public static void increment(int i)
    {
        System.out.println("i before increment in method : " + i);
        i++;
        System.out.println("i after increment in method : " + i);
    }
}
OUTPUT

i before increment : 5
i before increment in method : 5
i after increment in method : 6
i after increment : 5

DESCRIPTION

In this program we have defined a increment method and calling it from the main method. increment is the called method, where as the main is the calling method. Here we passed the parameter i using pass by value method. As we can see here the value of i is not changed in the main method. The value which is incremented to 6 in the method increment is not reflected in the calling method main. So, any changes done in the called method are not reflected in the calling method.

THINGS TO TRY
  • Change the return type of increment method from void to int and return the incremented value. In the main method, change the code increment(i); to i = increment(i);. Observe that the value incremented in the called method is now reflected in the calling method main.
As we found in the previous program, when the parameter is passed by value, the changed value is not reflected in the calling method. The below program shows how we can pass by reference and get the changed values reflected in the calling method.
Pass By Reference
class PassByReference
{
    public static void main(String args[])
    {
        Student sMain = new Student();
        sMain.name = "Jagan";
        sMain.marks = 93;
        sMain.section = 'B';
    
        System.out.println("Marks before incrementing : " + sMain.marks);
        incrementMarks(sMain);
        System.out.println("Marks after incrementing : " + sMain.marks);
    }
    
    public static void incrementMarks(Student sMethod)
    {
        System.out.println("Marks before incrementing in the method: " + sMethod.marks);
        sMethod.marks = sMethod.marks + 1;
        System.out.println("Marks after incrementing in the method: " + sMethod.marks);
    }
}

class Student
{
    String name;
    int marks;
    char section;
}
OUTPUT

Marks before incrementing : 93
Marks before incrementing in the method: 93
Marks after incrementing in the method: 94
Marks after incrementing : 94

DESCRIPTION

Here we created a method called incrementMarks which takes a Student object as a parameter. Since the Student is not a primitive data type, the object is passed by reference. When the marks are incremented in the incrementMarks method, the marks are also reflected in the main method. That is why when the marks are printed after incrementing, the value 94 is printed.

THINGS TO TRY
  • In the incrementMarks method, change the section to 'Z'. In the main method print the section before and after calling the incrementMarks method and observe that the value changes from 'B' to 'Z'.
Please note that only when the values in the non primitive data type are changed using the dot(.) operator, the value is reflected in the calling method. If the reference itself is overwritten as shown below (LINE A), then the changes will not be reflected in the main method. This is because, as discussed in Java Objects References, when the reference itself is overwritten, the object it points to is changed. But the reference in the main method still points to the old object and any changes done to the new object will not impact the old object.
public static void incrementMarks(Student sMethod)
{
    System.out.println("Marks before incrementing in the method: " + sMethod.marks);
    sMethod = new Student(); // LINE A
    sMethod.name = "Murugan";
    sMethod.marks = 75;
    sMethod.section = 'A';
    System.out.println("Marks after incrementing in the method: " + sMethod.marks);
}

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App