Java编程语言和Java指南中关于Java的参数传递的解释

来源:互联网 发布:哈尔滨医科大学知乎 编辑:程序博客网 时间:2024/05/16 01:43

From The Java Programming Language, by James Gosling et al. 3rd edition (pg.
56):
quote:
Some people will say incorrectly that objects are passed "by reference."In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked
function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in
memory. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made
through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.


From The Java Tutorial
quote:
Pass by Value
In Java methods, arguments are passed by value. When invoked, the method receives the value of the variable passed in. When the argument is of primitive type, pass -by-value means that the method
cannot change its value. When the argument is of reference type, pass-by-value means that the
method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object. This is often the source of confusion--a rogrammer writes a method that attempts to modify the value of one its arguments and the method doesn't work as expected.
Let's look at such method and then investigate how to change it so that it does what the programmer originally intended. 

原创粉丝点击