开发者

Pass by value or Pass by reference in Java?

开发者 https://www.devze.com 2023-04-02 15:14 出处:网络
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..

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);

开发者_StackOverflow        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.


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.


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.


You can think of pass-by-value as passing only the value contained in the variable and not the variable itself. So then the value is copied into another temporary variable. In contrast, you can think of pass-by-reference as passing the actual variable and no temporary copies are made, so that any changes in the variable is 'saved'. Although there is more to it, it might be easier way to thinking about it


I think instead creeping around if Java supports pass by reference or values, one should be clear about the way of using the instances of the classes in Java in his implementation. It all happens to be the type of instances - mutable/immutable which is gonna decide the way we pass things to the functions! That's up to you to explore this difference!

Let me clarify my argument of why there is no need of chasing back at passing what?! Consider this... First Code:

    void foobar(int *a){
        printf("%d", *a);
    }

    int main(){
        int a = 5;
        foobar(&a);
        return 0;
    }

Here in this C code, what are you passing... the address of the variable 'a'. This happens to be the pass by reference! :)

Let us consider another one... Second Code:

    void foobar(int* a){
        printf("%d", *a);
    }

    int main(){
        int a = 5;
        int *p = &a;
        foobar(p);
        return 0;
    }

Here in this C code, what am I passing....? The value of the variable 'p', doesn't matter whether it is pointer or something :P

So what do you call this as pass by value/pass by reference? I leave this to you! But all we need to look at is how we gonna implement... :)

So in Java... with what we pass we can say - It supports "Pass by value" or "Pass by reference of some instance" and not "Pass by reference"

***Only thing which I can clearly conclude is with the primitive data types in Java. Since there is no pointers with which one can edit the content of a byte without the actual variable, we can't have pass by reference for them(I mean the primitive data types) in Java.

0

精彩评论

暂无评论...
验证码 换一张
取 消