Java is Strictly Pass by Value!
In order to understand more of how the java is processing the parameter in methods and functions, lets compare the java program with a C++ code which would make it more clear and helps you get the major difference between how the parameters are being passed to any methods or functions wrt passing parameters by value and with reference and it is the reason why Java is Strictly Pass by Value!.
Java
import java.io.*;
class GFG {
static int add( int x, int y)
{
{
x += y;
return x;
}
}
public static void main(String[] args)
{
int a = 1 , b = 2 ;
add(a, b);
System.out.println( "After the add function a = "
+ a);
System.out.println( "After the add function b = "
+ b);
}
}
|
Output
After the add function a = 1
After the add function b = 2
C++
#include <iostream>
using namespace std;
int add( int * x, int * y)
{
*x += *y;
return *x;
}
void sum( int x, int y)
{
x += y;
}
int main()
{
int a = 1, b = 2;
sum(a, b);
cout << "After sum function is a = " << a;
cout << "\n" ;
cout << "After sum function is b = " << b << "\n\n" ;
add(&a, &b);
cout << "After add function is a = " << a;
cout << "\n" ;
cout << "After add function is b = " << b;
return 0;
}
|
Output
After sum function is a = 1
After sum function is b = 2
After add function is a = 3
After add function is b = 2
From the above examples of C++ and Java programs, the parameters x and y are taking the copies of the value of a and b and performing the operation based on the copied values passed to the methods. As in all high language when ever a method or function is being parameterized they will only pass the values as copies to the methods or functions. Pointers are the efficient way of passing the parameters by reference and hence the values of the parameters are mutable as the operation being performed is on the address( memory location of variable ) rather than the value of variable.
Hence Java does not have pointers because it is a vulnerability of accessing the addresses directly and leads to major security issues and these are some of the reasons why it is considered as more secure.
Consider the following Java program that passes a primitive type to function.
Java
public class Main {
public static void main(String[] args)
{
int x = 5 ;
change(x);
System.out.println(x);
}
public static void change( int x) { x = 10 ; }
}
|
Output:
5
We pass an int to the function “change()” and as a result the change in the value of that integer is not reflected in the main method. Like C/C++, Java creates a copy of the variable being passed in the method and then do the manipulations. Hence the change is not reflected in the main method.
How about objects or references?
In Java, all primitives like int, char, etc are similar to C/C++, but all non-primitives (or objects of any class) are always references. So it gets tricky when we pass object references to methods. Java creates a copy of references and pass it to method, but they still point to same memory reference. Mean if set some other reference to object passed inside method, the object from calling method as well its reference will remain unaffected. The changes are not reflected back if we change the object itself to refer some other location or object If we assign reference to some other location, then changes are not reflected back in main().
Java
class Test {
int x;
Test( int i) { x = i; }
Test() { x = 0 ; }
}
class Main {
public static void main(String[] args)
{
Test t = new Test( 5 );
change(t);
System.out.println(t.x);
}
public static void change(Test t)
{
t = new Test();
t.x = 10 ;
}
}
|
Output:
5
Changes are reflected back if we do not assign reference to a new location or object: If we do not change the reference to refer some other object (or memory location), we can make changes to the members and these changes are reflected back.
Java
class Test {
int x;
Test( int i) { x = i; }
Test() { x = 0 ; }
}
class Main {
public static void main(String[] args)
{
Test t = new Test( 5 );
change(t);
System.out.println(t.x);
}
public static void change(Test t) { t.x = 10 ; }
}
|
Output:
10
Exercise: Predict the output of following Java program
Java
class Main {
public static void swap(Integer i, Integer j)
{
Integer temp = new Integer(i);
i = j;
j = temp;
}
public static void main(String[] args)
{
Integer i = new Integer( 10 );
Integer j = new Integer( 20 );
swap(i, j);
System.out.println(" i = " + i + "
, j = " + j);
}
}
|
Last Updated :
03 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...