`

Java call by value 和call by reference 的理解

    博客分类:
  • java
 
阅读更多
    Java中参数的传递有两种,一种是按值传递(传递的是具体的值,如基础数据类型),另一种是按引用传递(传递的是对象的引用,即对象的存储地址)。
main()
{
  //按值传递
  int val = 20 ;
  print(val) ; //打印的是20
  setVal(val); 
  print(val) ; //打印的还是20
  //按引用传递
  StringBuffer val = new StringBuffer("234");
  print(val.toString()) ;//打印的是234  
  setStringVal(val) ;
  print(val.toString()) ;//打印的是234  
  changeStringVal(val);
  print(val.toString()) ;//打印的是234123
}

void setVal(int val)
{
  val = 10 ;
}
void setStringVal(StringBuffer val)
{
  val =  new StringBuffer("345");
}
void changeStringVal(StringBuffer val)
{
  val = val.append("123") ;
}

从上面的代码可以了解:
1.按值传递时在调用的方法中,参数只是实际参数的一个拷贝。所以不管参数在方法里面如何修改都不会改变原来参数值。也就是说val的值一直保持不变。
2.按引用传递时会产生一份新的引用(不妨叫做y)。此时x和y指向了同一个对象。当调用setStringVal方法时引用拷贝即y指向了另一个对象("345")此时并不会改变x引用的值,所以打印的是234。当调用changeStringVal时y引用改变了对象的实际的值,此时x和y指向的还是同一个对象。所以打印的是234123。

从上面的分析我们可以得出以下结论:
1.call by value不会改变实际参数的数值。
2.call by reference不能改变实际参数的参考地址。
3.call by reference能改变实际参数的内容。

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    详解Java的call by value和call by reference

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

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

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

    Java邮件开发Fundamentals of the JavaMail API

    Presented by developerWorks, your source for great tutorials ibm.com/developerWorks Table of Contents If you're viewing this document online, you can click any of the topics below to link ...

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

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

    java 面试题 总结

    JAVA平台提供了两个类:String和StringBuffer,它们可以储存和操作字符串,即包含多个字符的字符数据。这个String类提供了数值不可改变的字符串。而这个StringBuffer类提供的字符串进行修改。当你知道字符数据要改变...

    Java 语言基础 —— 非常符合中国人习惯的Java基础教程手册

    (2) 类名是由程序员自己定义的 Java 标识符,每个类说明必须有 class 和类名。 (3) 类说明修饰符包括:  abstract 说明一个类为抽象类,抽象类是指不能直接实例化对象的类。  final 说明一个类为最终类,即...

    带注释的Bootstrap.java

    * Daemon reference. */ private Object catalinaDaemon = null; protected ClassLoader commonLoader = null; protected ClassLoader catalinaLoader = null; protected ClassLoader sharedLoader = null; ...

    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 ...

    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.......... . ...

    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....

    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 ...

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

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

    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, ...

    JavaScript权威指南

    By Value Versus by Reference Section 11.3. Garbage Collection Section 11.4. Lexical Scoping and Nested Functions Section 11.5. The Function( ) Constructor and Function Literals Section 11.6. ...

Global site tag (gtag.js) - Google Analytics