Call By Value in Java
Call by value in Java
There are two ways we can call a method in Object oriented programming:-
1. Call by value
2. Call by reference
In java, we only used call by value mechanism. Let's first understand how an object or variable is stored in java.
1. Primitive types :- These are value types. It stores value directly inside a memory.
2. Non-primitive or reference types :- These are reference types. It stores reference inside a memory not an object. The actual object is created somewhere else in heap memory. Here we are only storing reference of this object as a value which points out that object in heap.
The above example shows how the integer value of a variable is stored but for a reference variable it stores the memory information of an object which is created inside the heap memory.
Call by Value
Call by value is an approach where a method copies the value of an argument( actual parameter) into a formal parameter of that function.
When we pass any primitive datatype variable in a method parameter. It does not send the original variable, It creates a local copy of that variable with same value and then used this copied variable in a method and If we make any changes to a variable inside a method it will not reflect outside the scope of a method. The primitive datatype are always passed as call by value.
So the question arises what happened when we passed Non-primitive data types or reference as argument in method?
In, Call by reference we are passing the address of that variable as argument in the method. In C, C++ there is a concept called pointer but in Java we don't have pointers. That's why Java doesn't support call by reference.
Then how can we send a reference in a method?
In Java, We don't know the actual address of an Object like C, C++. The memory locations are irrelevant to programmers. So whenever an object is created with a new keyword, it will assign memory to the object in heap and provide us some information about where the object is located, the variable that stores this information is called reference variable. When we try to print the value of a reference variable, the output contains <CLASS_NAME> concatenated by @ and the hashcode created by hashCode method in Object class.
Let suppose there is one class Test and there are two reference variables X and Y. If I copy location information of X into Y then both X and Y refer to the same object. The same concept we used while passing reference variables in method.

When we pass any non-primitive datatype variable in a method as an argument. The value of the reference variable(actual parameter) is copied into a formal parameter of a method. Now both actual parameter and formal parameter points out the same object. So, whatever we change inside a method in that object, the changes are reflected outside a scope of method also. Because formal parameter and actual parameter both are pointing a same object in heap memory
Below example shows how call by value works for primitive and non primitive or reference variable
Comments
Post a Comment