Java pass by reference or value

Does Java pass by reference or pass by value?
         Java doesn't support the call by reference but it supports the call by value.

what is call by value and call by reference in java?
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/pass 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/pass 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 print(int x)//passing primitive data type
{
x = 50;
}
public static void main(String args[])
{
CallByValueExample c = new CallByValueExample();
int y = 100;
c.print(y);
System.out.println(y);//original value will not changed
}
}

Output : 100

In the above example, The print() method takes a parameter of primitive types i.e int. The variable y value 100 is passed to the parameter of x of print() 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("llo");//using append() method of StringBuffer Class
System.out.println(s2);//will print Hello
}
public static void main(String args[])
{
CallByReferenceExample c = new CallByReferenceExample();
StringBuffer s1 = new StringBuffer("He");
c.show(s1);
System.out.println(s1);//will print Hello
}
}


Output : Hello            
             Hello

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 Hello.

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.


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation