Home  >  Article  >  Java  >  Analyze whether there is reference transfer in java

Analyze whether there is reference transfer in java

零到壹度
零到壹度Original
2018-03-19 09:53:181544browse

There have been different opinions on whether there is reference passing when calling functions in Java. Some people say that Java only has value transfer, while others say that Java has both value transfer and reference transfer. So, is there reference transfer in Java? Let me analyze it below.

Value transfer: It is a copy of the passed parameters. The modification of the parameters is only a modification of the copy. The function call ends, the copy is discarded, and the original variables remain unchanged (that is, the actual parameters remain unchanged)

Pass by reference: When parameters are passed to a function, no copy is made, but the parameters themselves are directly passed into the function. Any changes to the parameters within the function will be reflected in the original variables.

Both C++ and Java have the concept of reference, but they have completely different meanings in these two languages. In C++, we can use the form "int &b=a" to define a reference b of variable a. b is just an alias of a. b and a occupy the same storage unit in memory. Using the reference mechanism, we can call Functions implement two-way transfer of values ​​- that is, transfer by reference. See the following code:

 
 示例一 
     #include <iostream> 
     using namespace std;
     int main() 
     {   void swap(int &,int &);
       int i=3,j=5; 
         swap(i,j); 
            cout<<"i="<<i<<"j="<<j<<endl; 
               return 0; 
               } void swap(int &a,int &b)
                 { int temp;
                   temp=a; 
                     a=b;  b=temp;  }
  执行上面的程序输出的是i=5 j=3,a和b传递给swap()函数的时候,是传递的他们本身的地址,不是他们的拷贝,所以在函数中对他们的改变可以直接影响到实参a和b,这就是引用传递。 java中的引用更像C++中的指针,当我们定义一个对象时(比如Person p=new Person()),定义的对象实例会放到java堆中,而变量p(即引用)会放到java栈中,p指向堆中的Person对象实例。
    
    为什么有很多人认为java有引用传递呢?一种情况是有人认为调用函数时其参数有可能是引用(如上面的p),所以java有引用传递,这部分人对引用传递根本没有正确的认识;而另一种情况看似有道理,但是仔细分析也是不正确的的,他们往往会用如下的代码来证明他们的观点: 
    实例二:
    复制代码 class Demo{  
       int a;  
       public Demo(int a){  
           this.a=a;  
       }  
   }  
   public class TestQuote{  
       public static void main(String args[]){  
           Demo d1=new Demo(1);  
           Demo d2=new Demo(2);  
           System.out.println(d1.a);  
           System.out.println(d2.a);  
           function(d1,d2);  
           System.out.println(d1.a);  
           System.out.println(d2.a);  
       }  
       private static void function(Demo d1,Demo d2){  
           int a;  
          a=d1.a;  
           d1.a=d2.a;  
           d2.a=a;  
       }  
   }  
  复制代码 
   他们的观点如下:执行上面的代码,调用function()函数以前输出的结果是1、2,调用function()函数之后输出的结果会是2、1,可见在函数内对d1和d2的改变反映到了原来的变量上,要不是不会输出2、1的。  这种解释是很迷惑人的,看上去好像很正确,下面的代码会很好的反驳上面的观点:
    示例三:
     复制代码  class Demo{  
       int a;  
      public Demo(int a){  
           this.a=a;  
       }  
   }  
   public class TestQuote{  
      public static void main(String args[]){  
          Demo d1=new Demo(1);  
          Demo d2=new Demo(2);  
          System.out.println(d1.a);  
          System.out.println(d2.a);  
          function(d1,d2);  
          System.out.println(d1.a);  
          System.out.println(d2.a);  
      }  
      private static void function(Demo d1,Demo d2){  
          Demo temp;  
          temp=d1;  
          d1=d2;  
          d2=temp;  
      }  
  } 
   执行上面的代码,调用function()前后程序输出的都是1、2,此程序试图通过调用function()交换d1和d2,但是没有成功,为什么呢?因为d1和d2是值传递,function()中的d1和d2是main()函数中d1和d2的副本,调用完function()不会对main()中的变量产生影响。再看示例二中,function()函数内改变的并不是d1和d2本身的值,而是d1和d2指向的对象的值,调用完function()后d1和d2仍然指向函数调用前的堆地址,即函数参数是栈中的d1和d2,而不是堆中d1和d2指向的对象,即使你在函数中改变了堆中的对象,但没有改变函数参数的值。所以示例二并不是什么引用传递;可见java中只有值传递。
   但是网上有很多针对"java值传递和引用传递进行的区别"的文章,如果读者看到的话一定要清楚,其中所说的引用传递是不正确的,他们所说的引用传递就是示例二中的那种情况。不幸的是,网上有很多文章都把示例二中的那样的传递看做引用传递,如果读者看到知道是什么意思就行了。

The above is the detailed content of Analyze whether there is reference transfer in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn