通过Java传递值或通过引用传递? [英] Pass by value or Pass by reference in Java?

查看:96
本文介绍了通过Java传递值或通过引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多文章,并且都说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);

   }

}

执行

800
800 and 
100

任何人都可以向我解释这些问题......

Can anyone explain me these questions...


  1. 什么是按价值传递是指...

答案:它只是将存储在变量中的数字或值传递给函数。我是对还是错。

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


  1. 你怎么说Java是通过价值?

  2. 为什么Java是Pass By Value而不是引用?

  3. 上面的程序Tries是否显示了Pass by value和Reference ...的例子,但仍然只通过Pass by Value来做事情。我写了那个程序。


推荐答案

你的答案是对的,但你缺乏详情。如果你这样做

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

狗d =新狗()

d 是对Dog实例的引用,即值传递意味着您在传递 d 进入方法

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

将引用的a 副本(即引用的值,而不是引用本身)传递给方法。因此,您有两个对Dog的一个实例的引用,一个在调用范围中,一个在被调用范围内。让我们说在 walkDog 方法中有一行

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

方法中的 d 引用指向新狗。首次调用该方法的原始引用仍然指向原始 Dog。如果Java通过引用传递,则相同的引用,而不是引用的副本将在方法中使用,因此更改引用的值将影响调用和被调用的引用范围。

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

walkDog 方法中, 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.

这篇关于通过Java传递值或通过引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆