`

call by value 和call by reference 部分转载

    博客分类:
  • JAVA
 
阅读更多

关于JAVA中参数传递问题有两种,一种是按值传递(如果是基本类型),另一种是按引用传递(如果是對象).

 

首先以两个例子开始:

1)
public class Test2 {

public static void main (String [] args) {
StringBuffer a = new StringBuffer ("A");
StringBuffer b = new StringBuffer ("B");
operate (a,b);
System.out.println(a+","+b);
}

static void operate(StringBuffer x, StringBuffer y){
     x.append(y);
     y = x;
   }
}
输出:AB,B

2)
public class Test2 {

public static void add3 (Integer i){
      int val=i.intValue();
      val += 3;
      i = new Integer (val);
}

public static void main (String args [ ] ) {
Integer i = new Integer (0);
add3 (i);
System.out.println (i.intValue ( ));
}
}
输出:0

首先我们应该明白JAVA中的参数传递全是以值传递的。是基本类型,就拷贝一个基本类型传进方法;是引用,就拷贝一个引用变量传进去方法,理解了这 两点就能理解方法操作对象的相关问题了。最好能画出引用指向对象的图出来,就能完全理解了。
第1题,调用operate方法时,传入了两个引用a,b的拷贝x,y,这两个x,y都指向原a,b引用所指向的对象。x.append(y)对它指向的 对象(即a指向的对象)进行了操作。而x=y,只是两个拷贝变量在赋值,并没有影响到原b所指向的对象。所以b所指向的对象仍然为B。
第2题,i=new Integer(val)只是一个引用的拷贝指向了另外一个对象,而原来的i仍然是指向对象new Integer(0)的。
把握住了JAVA都是传值并且传的都是拷贝的话,类似的题大家都能迎刃而解了。

 

Java中的参数传递只有一种方式: by value. 理论说教太麻烦了,直接看些例子吧:
1). 基本类型
    public class A{
   public static void main(String[] args){
       int x = 1;
       System.out.println(x);    //1
       test(x);
       System.out.println(x);    //还是1==>By value
   }
  
   static void test(int a){
      a = 2;
   }
}

2). 引用类型
    public class B{
   public static void main(String[] args){
       Integer x = new Integer(1);
       System.out.println(x);
       test(x);
       System.out.println(x);
      
   }
  
   static void test(Integer a){
      a = new Integer(2);
   }
}

理解这里的关键是区分对象和引用。 这里声明的x是一个引用,而不是一个对象(只是Java把它设计为看上去好像是对象一样)。这个引用它指向了一个对象,这个对象就是后面用new关键字生 成的对象。因此,可以说x指向了一个Integer对象。
在调用test方法的时候,程序将x作为参数传递给test方法了。这里仍然是值传递在test调用过程中,会产生一份新的引用(不妨叫做y)。此 时,x和y指向了同一个对象。
   x和y指向的是同一个对象, 由于Java的设计,我们可以通过操作引用来达到操作对象的目的。因此,如果我们此时使用y来修改对象的属性 (例如,y.someField++); 你可以看到x指向的对象同时也被修改到了。
   另一方面,如果我们让y指向另外一个对象, y=new Integer(2); 此时x和y就指向了不同的
对象。y修改了它指向的对象的属性,很显然不会影响到x指向的对象

 

有人说了数组。数组也是一个引用类型,它的参数传递方式按照引用类型的参数传递一样可以解释得通:

import java.util.Arrays;

public class A{
   public static void main(String[] args){
      int[] aa = {3, 2, 1};
      System.out.println(Arrays.toString(aa)); //[3, 2, 1]
      test(aa);
      System.out.println(Arrays.toString(aa)); //[3, 2, 1]
      test2(aa);
      System.out.println(Arrays.toString(aa)); //[4, 2, 1]
   }
  
   static void test(int[] a){
      a = new int[]{1, 2, 3};   //指向了新对象
   }
  
   static void test2(int[] a){
     if(a != null && a.length > 0)
       a[0]++;              //修改原来的那个对象
   }
}

 

 

对象是传引用(call by reference),简单类型是传值(call by value),不要被网上的一些概念所迷惑!!!你可以自己做个试验。
至于String等类型传的还是引用。如果你用concat方法,String对象的原值就会被改变。
但你如果按如下方法:
public class Test {
    public static void test(String str) {
        str = "World";
    }
    public static void main(String[] args) {
        String string = "Hello";
        test(string);
        System.out.println(string);
    }
}

  运行结果:Hello
这里str = "World" 就等同于 String str=new String("World")。所以结果没有改变!!!

下列程序在1处是否会有异常,如果没有,输出是什么?是否会运行到2处,如果会,输出是什么?为什么会有这样的结果?

 

  import java.util.arraylist;
   import java.util.list;
  
   public class testclass {


   public static void main(string args[]) {
     list list = new arraylist();
     test2(list);
     system.out.println(list.size()); // 1处
     test3(list);
     system.out.println(list.size()); // 2处
   }
  
   public static void test2(list list) {
     list = null;
   }
  
   public static void test3(list list) {
      list.add(“aaaa“);
   }
   }

plumechen:

不会出错的。结果是0,1。

因为test2(list)传得是list的引用,我理解成指针置的副本,list=null;只是把那个传入的值设置为null,不改变原来 list的指针和内容。test3(list)传入的一样,但是执行了list.add()由于传入指针值的副本也指向原来的那个list的地址,所以原 来的那个list的内容就改变了,size变成了1了

 

 

 6.下列正确的有()

  A. call by value不会改变实际参数的数值

  B. call by reference能改变实际参数的参考地址

  C. call by reference不能改变实际参数的参考地址

  D. call by reference能改变实际参数的内容

  答案:ACD

 

 

Java中的参数传递只有一种方式: 值传递 (by value)

1、简单类型类型,在调用的方法中,直接是实参的一个拷贝

2、对象:程序将对象x作为参数传递给方法了。这里仍然是值传递在方法调用过程中,会产生一份新的引用(不妨叫做y)。此时,x和y指向了同一个对象。

我们可以通过操作引用来达到操作对象的目的。 因此,如果我们此时使用y来修改对象的属性 (例如,y.someField++); 你可以看到x指向的对象同时也被修改到了。(值传递,在方法里产生引用,操作引用达到操作对象。所以引用可以修改实参(对象)的内容)

 

实验

 

package callbyvalue;

//public class Test2 {
//
//    public static void main (String [] args) {
//    StringBuffer a = new StringBuffer ("A");
//    StringBuffer b = new StringBuffer ("B");
//    operate (a,b);
//    System.out.println(a+","+b);          //AB,B
//    }
//
//    static void operate(StringBuffer x, StringBuffer y){  //JAVA中的参数传递全是以值传递的。
//        //是基本类型,就拷贝一个基本类型传进方法;是引用,就拷贝一个引用变量传进去方法
//         x.append(y);  //x.append(y)对它指向的对象(即a指向的对象)进行了操作
//         y = x;    //只是两个拷贝变量在赋值      并不会影响原来的对象
//         System.out.println(y);    //AB
//       }
//    }


//public class Test2 {
//
//    public static void add3 (Integer i){
//          int val=i.intValue();
//          val += 3;
//          i = new Integer (val);  //只是一个引用的拷贝指向了另外一个对象,而原来的i仍然是指向对象new Integer(0)的
//    }
//
//    public static void main (String args [ ] ) {
//    Integer i = new Integer (0);
//    add3 (i);
//    System.out.println (i.intValue ( ));  //0
//    }
//    }



//public class Test2 {  //我的实验
//
//    public static void change (int i){
//        i=6;           //给拷贝赋值,并没有改变a指向的值
//    }
//
//    public static void main (String args [ ] ) {
//    int a=5;
//    change(a);
//      System.out.println (a);  //5   JAVA都是传值并且传的都是拷贝的话
//    }
//    }


//public class Test2 {  //我的实验
//
//    public static void change (int i){
//        i++;           //对象是传引用(call by reference),简单类型是传值(call by value)
//                       //传值(call by value)不会改变实际参数的数值
//    }
//
//    public static void main (String args [ ] ) {
//    int a=5;
//    change(a);
//      System.out.println (a);  //5  
//    }
//    }

分享到:
评论

相关推荐

    详解Java的call by value和call by reference

    在本篇文章里小编给大家总结了关于Java的call by value和call by reference的相关用法和知识点内容,需要的朋友们学习下。

    JavaScript中的值是按值传递还是按引用传递问题探讨

    在分析这个问题之前,我们需了解什么是按值传递(call by value),什么是按引用传递(call by reference)。在计算机科学里,这个部分叫求值策略(Evaluation Strategy)。它决定变量之间、函数调用时实参和形参之间值是...

    C++传值调用与引用调用区别实例代码

    C++ comparing call_by_value(传值调用) and call_by_reference(引用调用)

    JS是按值传递还是按引用传递

    按值传递(call by value)是最常用的求值策略:函数的形参是被调用时所传实参的副本。修改形参的值并不会影响实参。   按引用传递(call by reference)时,函数的形参接收实参的隐式引用,而不再是副本。这意味着函数...

    深入浅析JS是按值传递还是按引用传递(推荐)

    按值传递(call by value)是最常用的求值策略:函数的形参是被调用时所传实参的副本。修改形参的值并不会影响实参。  按引用传递(call by reference)时,函数的形参接收实参的隐式引用,而不再是副本。这意味着函数...

    数位板压力测试

    16- and 32-bit API Reference By Rick Poyner Revised February 11, 2012 This specification was developed in response to a perceived need for a standardized programming inter-face to digitizing tablets...

    Swift.Pocket.Reference.Programming.for.iOS.and.OS.X.2nd.Edition.149194

    Updated to cover the latest features in Swift 2.0, this pocket reference is the perfect on-the-job tool for learning Swift’s modern language features, including type safety, generics, type inference...

    Bochs - The cross platform IA-32 (x86) emulator

    Changes in 2.4.6 (February 22, 2011): Brief summary : - Support more host OS to run on: - Include win64 native binary in the ... [2914433] makesym.perl misses symbols by John R. Jackson [2908481] USB ...

    The C programming Language(chm格式完整版)

    Arguments - Call by Value Character Arrays External Variables and Scope Chapter 2: Types, Operators and Expressions Variable Names Data Types and Sizes Constants Declarations Arithmetic ...

    SystemVerilog Reference Manual 3.1a(中英文版)+最新SV IEEE 标准

    2.2 Literal value syntax..........................................................................................................................4 2.3 Integer and logic literals ........................

    Effective C++(第三版)

    条款20:宁以pass-by-reference-to-const替换pass-by-value prefer pass-by-reference-to-const to pass-by-value. 条款21:必须返回对象时,别妄想返回其reference don't try to return a reference when you must ...

    php.ini-development

    directive because it is not set or is mistyped, a default value will be used. ; The value can be a string, a number, a PHP constant (e.g. E_ALL or M_PI), one ; of the INI constants (On, Off, True, ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Other C++ Features Reference Arguments Function Overloading Default Arguments Variable-Length Arrays and alloca() Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and ...

    Microsoft Library MSDN4DOS.zip

    CHAPTER 7 PASSING BY REFERENCE OR VALUE CHAPTER 8 NUMERICAL, LOGICAL, AND STRING DATA CHAPTER 9 SPECIAL DATA TYPES Index Microsoft Codeview and Utilities User's Guide Table of Contents Introduction ...

    LINQPad_Premium_5.36.03_Any_CPU 含破解

    When using LPRun to execute scripts via the command-line, you can now control the %errorlevel% explicitly - either by returning an integer value from Main or by setting Environment.ExitCode....

    [麻省理工学院-算法导论].Introduction.to.Algorithms,.Second.Edition

    Chapter 3 precisely defines this notation, which we call asymptotic notation. It starts by defining several asymptotic notations, which we use for bounding algorithm running times from above and/or ...

    servlet2.4doc

    The default behavior of this method is to call addIntHeader(String name, int value) on the wrapped response object. addIntHeader(String, int) - Method in interface javax.servlet....

    Addison.Wesley.C++.by.Dissection.2002.pdf

    3.6 Call-By-Value......... . 81 3.7 Recursion......... . . . 83 3.8 Default Arguments........ 84 3.9 Functions as Arguments....... 86 3.10 Overloading Functions........ . 88 3.11 Inlining.......... . ...

    微软内部资料-SQL性能优化3

    For an example of how to decode value from this column using the information above, let us assume we have the following value: 0x000705001F83D775010002014F0BEC4E With byte swapping within each ...

Global site tag (gtag.js) - Google Analytics