您好,登錄后才能下訂單哦!
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.Quiz question from Sun:
In Java programming, all method parameters are passed by value.
Answer: true In Java, everything is pass-by-value, unless you think Dr. James Gosling forgot how he invented Java, AND Sun does not know how Java works.If we all agree those are not the cases. Then the question should change to that how to understand the fact "In Java, everything is pass-by-value".If you go up-and-down a little, you will find a lot of help here.Two examples to prove:
Example one:(from:http://bobcat.webappcabaret.net/javachina/faq/07.htm#pas_Q2)
// PassByValueTest.java public class PassByValueTest { public static void main(String [] args) { String arr[]=new String[2]; arr[0]="hello"; arr[1]="hi"; // nothing will change here swap(arr[0], arr[1]); System.out.println(arr[0] + ", " + arr[1]); //hello, hi // two Strings are actually swapped swap(arr, 0, 1); System.out.println(arr[0] + ", " + arr[1]); //hi, hello } // useless swap method, since you swap the copies // of two strings' reference inside the method // Strings outside are not affected. public static void swap(String s1,String s2){ String tmp = null; tmp = s1; s1 = s2; s2 = tmp; } // real practical swap here // swap array elements with two indices // very useful for sorting algorithm, etc. // reference of arr will never change (pass-by-value) // but the contents of arr will change permanently // since the copy of arr reference still refer to the same object. public static void swap(String arr[], int ix1, int ix2) { String tmp = null; tmp = arr[ix1]; arr[ix1] = arr[ix2]; arr[ix2] = tmp; } }Example two(my code):
public class ParamTest {
/**
* @param args
* @author Jianming
* @version 1.0
*/public static void main(String[] args){
/*
* Test one:Methods can't modify numeric parameters
*/
System.out.println("Testing tripleValue:");
double percent = 10;
System.out.println("Before: percent = " + percent);
tripleValue(percent);
System.out.println("After: percent = " + percent);
/*
* Test two: Methods can modify the state of object parameters
*/
System.out.println("nTesting tripleSalary:");
Employee harry = new Employee("Harry", 5000);
System.out.println("Before: salary = " + harry.getSalary());
tripleSalary(harry);
System.out.println("After:salary = " + harry.getSalary());
/*
* Test three:Methods can't attach new objects to object parameters
*/
System.out.println("nTesting swap:");
Employee a = new Employee("Jianming", 10000);
Employee b = new Employee("James", 20000);
System.out.println("Before: a = " + a.getName());
System.out.println("Before: b = " + b.getName());
swap(a, b);
System.out.println("After: a = " + a.getName());
System.out.println("After: b = " + b.getName());}
public static void tripleValue(double x){
x = 3 * x;
System.out.println("End of method:x = " + x);
}
public static void tripleSalary(Employee x){
x.raiseSalary(200);
System.out.println("End of method:salary = "
+ x.getSalary());
}
public static void swap(Employee x, Employee y){
Employee temp = x;
x = y;
y = temp;
System.out.println("End of method: x = " + x.getName());
System.out.println("End of method: y = " + y.getName());
}}
class Employee{
public Employee(String n, double s){
name = n;
salary = s;
}
public String getName(){
return name;
}
public double getSalary(){
return salary;
}
public void raiseSalary(double bypercent){
double raise = salary * bypercent / 100;
salary += raise;
}
private double salary;
private String name;}
The result:
Testing tripleValue:
Before: percent = 10.0
End of method:x = 30.0
After: percent = 10.0Testing tripleSalary:
Before: salary = 5000.0
End of method:salary = 15000.0
After:salary = 15000.0Testing swap:
Before: a = Jianming
Before: b = James
End of method: x = James
End of method: y = Jianming
After: a = Jianming
After: b = JamesNote:
(1):Methods can't modify the parameters of primitive type
(2):Methods can modify the state of object parameters,but methods can't attach new objects to object parameters
an example to explain the question:
電視機和遙控器可以很形象的描敘和解釋這個問題。
可以把遙控器看作是電視機的一個引用拷貝,只要電視機存在,也就是用遙控器對準一臺電視機,按遙控器上面的各種按扭(function)可以對電視機產(chǎn)生各種影響,但是你換一個遙控器對電視機來說是不會產(chǎn)生影響的,電視機并不會因為遙控器換了而變成別的電視機。同時遙控器也可以不對準原來的電視機,轉(zhuǎn)去對準別的電視機,這對原來的電視機也是不會產(chǎn)生影響的。一臺電視機可以有多個遙控器,并且只要某個遙控器對準了這臺電視機,這個遙控器就可以通過它上面的按扭改變電視機的狀態(tài)。a picture to show how it works:
End.
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。