Argument passing in JAVA

Argument passing in java is one of the most important topic to cover, Here I am going to elaborate that what happens when different types (primitive or objects) are passed into a method in JAVA. Generally, in any programming language there are only two ways argument passing( parameters passing) to the function:

  1. Pass – by – Value or call – by – value
  2. Pass – by – reference or call – by – reference

However, in Java the scenario is a bit different. The first approach is quite similar to other programming language like C/C++ but the second approach makes the JAVA different from others (C/C++). Okay, let’s have a closed at both the ways with examples.

  1. Pass – by – Value or call – by – value

In this approach the copies of the values of an argument is passed into the formal parameter of the function. Therefore, the changes made to the passed parameter value would not affect to the original copy. In JAVA, when primitive types variable (int, char, float, Boolean…) are passed, they follow the pass – by – value concept. Thus, what occurs to the parameter that receives the argument has no effect outside the method. Look into the below example:




class Test {

void meth(int i, int j) {

i = i * 2;

j = j*3;}

}

 

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 in the above example, the values of a and b are same before and after the calling the meth(). The operation inside meth() did not change the on the original copy of a and b.

 

  1. Pass – by – reference or call – by – reference

The situation changes when an object is passed to a method. The objects themselves are never passed to a method, but the objects are always in the heap and only a reference to the object is passed to the method. When any object is created as below:




A a = new A();

Behind the scene, a variable of the class type is created; you are creating a reference to the object. Thus, when this reference is passed 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 affect the object used as an argument. It may sound a bit confusing to C/C++ programmers because there is no pointers concept here and pointers directly deal with memory locations. But on the higher level if we see that no other copy of the passing object is being created as in the primitive types. Let’s take a look by an example:

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. And the changes are made into the original copies of the variable. So conceptually, if we can make the changes in the original copies only when it is passed as reference so it satisfies the same.

 

Reference: JAVA Complete Reference.

Read Similar Posts



Naman Singhal

As being an IT professional, I work as Quality Engineer.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.