Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass by value or Pass by reference in Java?

Tags:

java

I read many articles and all says Java is pass by value. But i still don't construe the difference between pass by value and reference. I wrote a sample program and it executes like this..

public class PassByValue {

   private int a;
   private int b;

   public PassByValue(int a, int b) {
    this.a = a;
    this.b = b;
   }

   public static int mul(int a, int b) {

       int z = a * b;
       System.out.println("The Value of Z inside mul: " + z);
       return z;

   }

   public static void main(String args[]) {

    int z = 100;

        int x = 40;
        int y = 20;

    mul(x,y);

    PassByValue pass = new PassByValue(x,y);
    mul(pass.a,pass.b);

        System.out.println("The Value of Z" + z);

   }

}

Execution

800
800 and 
100

Can anyone explain me these questions...

  1. What is Pass By Value means...

Answer: Its just passing the numbers or value stored in the variable to a function. Am i right or wrong.

  1. How do you say Java is Pass By Value?
  2. Why is Java is Pass By Value and not by reference?
  3. Does the above program Tries shows an example of Pass by value and Reference... but still does things via Pass by Value only... I wrote that program.
like image 751
theJava Avatar asked Sep 04 '11 19:09

theJava


Video Answer


2 Answers

The confusion is probably due to the fact that a variable can't contain an object in the first place. A variable can only contain a reference to an object. (In other words, objects aren't passed at all, not by reference, not by value.)

Once you realize this, it is quite clear that nothing is pass-by-reference in Java. A variable refering to an object stores a reference, and this reference is passed by value.


1. What is Pass By Value means...

Answer: Its just passing the numbers or value stored in the variable to a function. Am i right or wrong.

That's right. The value contained in the variable is passed, and not the variable itself.

1. How do you say Java is Pass By Value?

Java is pass by value because primitives are passed by value, and references are passed by value. (Objects are never passed.)

You can't implement a swap method in Java for instance. I.e., you can't do

String str1 = "hello";
String str2 = "world";
swap(str1, str2);
// str1 can't refer to anything but "hello"
// str2 can't refer to anything but "world"

2. Why is Java is Pass By Value and not by reference?

As explained above, even references are passed by value.

like image 178
aioobe Avatar answered Sep 20 '22 11:09

aioobe


You are right in your answer, but you are lacking detail. If you do

Dog d = new Dog()

d is a reference to an instance of Dog, i.e. What pass by value means is that you when pass d into a method

walkDog(d);

the a copy of the reference (i.e. the value of the reference, not the reference itself) to the Dog is passed into the method. So you have 2 references to the one instance of the Dog, the one in the calling scope, and the one in the called scope. Lets say in the walkDog method there is a line

d = new Dog();

the d reference in the method only points to the new Dog. The original reference where the method was first called still points to the original Dog. If Java had pass by reference, the same reference, not a copy of the reference would be used in the method, and so changing the value of the reference would affect the reference in both the calling and the called scope.

EDIT -- based on your comment, I want to make on thing clear. Since the reference in the original scope and the method scope both point to the same object, you can still change things on that object in both scopes. So if you do

d.drinkWater();

in the walkDog method, and drinkWater changes some variable on the dog object, then since both references point to the same object, which was changed.

It's only a distinction between what the references actually are in each scope. But it does come up a lot.

like image 45
hvgotcodes Avatar answered Sep 19 '22 11:09

hvgotcodes