Java pass by value or pass by reference - Java @ Desk

Friday, December 27, 2013

Java pass by value or pass by reference

Java "pass by value" or "pass by reference"

Before we come to conclusion lets understand both the basic terms with small examples

What is pass by value?
Pass by value means making a copy in memory of actual parameter's value that is passed. So when we pass the primitive as a parameter in java and if the parameter gets mofidied in the calling method, the caller method will still have the original value. This is because a copy of passed parameter is created in the memory

What is pass by reference?
It is also called pass by address, a copy of the address of the actual parameter is passed.

Example implementation for pass by value:
package test;

public class PrimitivePassByValue {

 public static void main(String args[]) {
  int originalNumber = 10;
  calledMethod(originalNumber);
  System.out
    .println("Original Value in caller method : " + originalNumber);
 }

 public static void calledMethod(int number) {
  number++;
  System.out.println("Modified value in called method : " + number);
 }

}

Output:
Modified value in called method : 11
Original Value in caller method : 10

Above output clearly shows that primitives are passed by value as parameters as explained below:
1) The "originalNumber = 10" that has been passed in the method "calledMethod()"
2) "calledMethod()" creates the copy of "originalNumber" value
3) "calledMethod()" increments the value by 1 and print it. So the value printed as 11
4) Execution returns back to "main" method. It prints the original value of "originalNumber" as 10

Best explanation I ever heard of pass by value from http://stackoverflow.com.
Say I want to share a web page with you.

If I tell you the URL, I'm passing by reference. You can use that URL to see the same web page I can see.
If that page is changed, we both see the changes.
If you delete the URL, all you're doing is destroying your reference to that page - you're not deleting the actual page itself.

If I print out the page and give you the printout, I'm passing by value.
Your page is a disconnected copy of the original.
You won't see any subsequent changes, and any changes that you make
(e.g. scribbling on your printout) will not show up on the original page.
If you destroy the printout, you have actually destroyed your
copy of the object - but the original web page remains intact.


Example implementation for pass by reference:


package test;

public class PassByReference {
 public static void main(String args[]) {
  PassByReferencePojo passByReferencePojo = new PassByReferencePojo(
    "Original Name");
  System.out.println("Original Name in called method before method call : "
    + passByReferencePojo.getFirstName());
  calledMethod(passByReferencePojo);
  System.out.println("Original Name in called method after method call : "
    + passByReferencePojo.getFirstName());
 }

 public static void calledMethod(PassByReferencePojo passByReferencePojo) {
  passByReferencePojo.setFirstName("Modified Name");
  System.out.println("Modified Name in called method : "
    + passByReferencePojo.getFirstName());
 }
}

Output:
Original Name in called method before method call : Original Name
Modified Name in called method : Modified Name
Original Name in called method after method call : Modified Name


1) PassByReferencePojo object is created with "First Name = Original Name"
2) PassByReferencePojo object is passed to "calledMethod"
3) "calledMethod" modifies the "First Name = Modified Name". This gets reflected globally
4) The caller method prints the value "First Name = Modified Name"

In java when an object is passed, in case with reference objects - copy of this reference created (and 2 references will point to one object). So - if you pass an object to method and inside the method - set null value to it (for example) - nothing will happen outside this method, because you just destroy the copy of passed reference. But - if you change some field of this object - these changes will be visible outside the method.

Just look at below implementation for making the passed object to null
package test;

public class PassByReference {
 public static void main(String args[]) {
  PassByReferencePojo passByReferencePojo = new PassByReferencePojo(
    "Original Name");
  System.out.println("Original Name in called method before method call : "
    + passByReferencePojo.getFirstName());
  calledMethod(passByReferencePojo);
  System.out.println("Original Name in called method after method call : "
    + passByReferencePojo.getFirstName());
 }

 public static void calledMethod(PassByReferencePojo passByReferencePojo) {
  passByReferencePojo = null;
 }
}

In this case, the output would be:
Original Name in called method before method call : Original Name
Original Name in called method after method call : Original Name

So - if you pass an object to method and inside the method - set null value to it (for example) - nothing will happen outside this method, because you just destroy the copy of passed reference.

Conslusion : Java is pass by value






No comments:

Post a Comment