Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Tuesday 1 August 2017

Call By Value and Call By Reference In Java


Now here we will discuss that what is call by value and call by reference in java.
Call By Value and Call by Reference In Java


Call by value is also known as pass by value and call by reference is also known as pass by reference.

Java doesn't support the call by reference but it supports the call by value.

Difference Between Pass by Value and Pass by Reference In Java

The call by value and call by reference are two mechanisms to pass parameters to the method of a program.

There is little difference between pass by value and pass by reference. 

Call By Value : If we call a method passing a value, This known as call by value or pass by value in java. The changes being done in the called method and is not affected in the calling method. Here original value will not be changed.

Call By Reference :  In this case, we pass the reference of the objects. Here original value will be changed.


Java Call By Value Example

public class CallByValueExample
{
public void show(int x)//passing primitive data type
{
x = 50;
}
public static void main(String args[])
{
CallByValueExample c = new CallByValueExample();
int y = 100;
c.show(y);
System.out.println(y);//original value will not changed 
}
}

Output : 100

In the above example, The show() method takes a parameter of primitive types i.e int. The variable y value 100 is passed to the parameter of x of show() method. As a copy of y is passed, changing x does not change the value of y in the main method of this program.

Now moving on to our next program which is call by reference.




Java Call By Reference Example

In this program, we will pass the reference of StringBuffer object in the show() method of this class.

public class CallByReferenceExample
{
public void show(StringBuffer s2)
{
s2.append("world");//using append() method of StringBuffer Class
System.out.println(s2);//will print HelloWorld
}
public static void main(String args[])
{
CallByReferenceExample c = new CallByReferenceExample();
StringBuffer s1 = new StringBuffer("Hello");
c.show(s1);
System.out.println(s1);//will print HelloWorld
}
}

Output : HelloWorld
              HelloWorld

In the given above example, Here the value of object reference s1 is passed to s2. In other words, s1's value "Hello" passed to s2. Now, s1 and s2 reference refer the same object. Changing s2 affects s1 also. Both s1 and s2 will print HelloWorld.

In short, In Java methods, we can pass primitive data types parameters and Object reference type parameters. Both are i.e primitive data types and Object reference data types are passed as value only.

When primitive data types are passed as methods parameters, they are passed by value(i.e a copy of the value) but in the case of the Object references, the reference is copied(here also a copy) and passed to the called method i.e object reference is passed as value. So the parameters copy and original reference both will refer the same object. So if the calling method changes the value of its reference, the original object value itself changes.

So finally, we can say that java does not support pass by reference, It supports only pass by value.

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate