91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java 函數的參數傳遞

發布時間:2020-08-07 19:44:27 來源:ITPUB博客 閱讀:135 作者:gehuiwin 欄目:編程語言

The Java programming language always uses call by value. That means that the method gets a copy of all parameter values. In particular, the method cannot modify the contents of any parameter variables that are passed to it.

[@more@]

For example, consider the following call:

double percent = 10;
harry.raiseSalary(percent);

No matter how the method is implemented, we know that after the method call, the value of percent is still 10.

Let us look a little more closely at this situation. Suppose a method tried to triple the value of a method parameter:

public static void tripleValue(double x) // doesn't work
{
   x = 3 * x;
}

Let's call this method:

double percent = 10;
tripleValue(percent);

However, this does not work. After the method call, the value of percent is still 10. Here is what happens:

  1. x is initialized with a copy of the value of percent (that is, 10).

  2. x is tripled—it is now 30. But percent is still 10 (see Figure 4-6).

    Figure 4-6. Modifying a numeric parameter has no lasting effect

    java 函數的參數傳遞

  3. The method ends, and the parameter variable x is no longer in use.

There are, however, two kinds of method parameters:

  • Primitive types (numbers, Boolean values)

  • Object references

You have seen that it is impossible for a method to change a primitive type parameter. The situation is different for object parameters. You can easily implement a method that triples the salary of an employee:

public static void tripleSalary(Employee x) // works
{
   x.raiseSalary(200);
}

When you call

harry = new Employee(. . .);
tripleSalary(harry);

then the following happens:

  1. x is initialized with a copy of the value of harry, that is, an object reference.

  2. The raiseSalary method is applied to that object reference. The Employee object to which both x and harry refer gets its salary raised by 200 percent.

  3. The method ends, and the parameter variable x is no longer in use. Of course, the object variable harry continues to refer to the object whose salary was tripled (see Figure 4-7).

    Figure 4-7. Modifying an object parameter has a lasting effect

    java 函數的參數傳遞

As you have seen, it is easily possible—and in fact very common—to implement methods that change the state of an object parameter. The reason is simple. The method gets a copy of the object reference, and both the original and the copy refer to the same object.

Many programming languages (in particular, C++ and Pascal) have two methods for parameter passing: call by value and call by reference. Some programmers (and unfortunately even some book authors) claim that the Java programming language uses call by reference for objects. However, that is false. Because this is such a common misunderstanding, it is worth examining a counterexample in detail.

Let's try to write a method that swaps two employee objects:

public static void swap(Employee x, Employee y) // doesn't work
{
   Employee temp = x;
   x = y;
   y = temp;
}

If the Java programming language used call by reference for objects, this method would work:

Employee a = new Employee("Alice", . . .);
Employee b = new Employee("Bob", . . .);
swap(a, b);
// does a now refer to Bob, b to Alice?

However, the method does not actually change the object references that are stored in the variables a and b. The x and y parameters of the swap method are initialized with copies of these references. The method then proceeds to swap these copies.

// x refers to Alice, y to Bob
Employee temp = x;
x = y;
y = temp;
// now x refers to Bob, y to Alice

But ultimately, this is a wasted effort. When the method ends, the parameter variables x and y are abandoned. The original variables a and b still refer to the same objects as they did before the method call (see Figure 4-8).

Figure 4-8. Swapping object parameters has no lasting effect

java 函數的參數傳遞

This discussion demonstrates that the Java programming language does not use call by reference for objects. Instead, object references are passed by value.

Here is a summary of what you can and cannot do with method parameters in the Java programming language:

  • A method cannot modify a parameter of primitive type (that is, numbers or Boolean values).

  • A method can change the state of an object parameter.

  • A method cannot make an object parameter refer to a new object.

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

安仁县| 惠东县| 孝感市| 横峰县| 平利县| 新乡县| 时尚| 三明市| 定安县| 上高县| 泸定县| 青河县| 临汾市| 华安县| 大悟县| 武冈市| 长沙市| 建阳市| 辽宁省| 永济市| 连山| 疏勒县| 弥勒县| 瓮安县| 宁陵县| 金湖县| 榆中县| 渭源县| 庆元县| 伊宁市| 顺昌县| 安平县| 汨罗市| 瑞丽市| 汾阳市| 台山市| 古蔺县| 怀仁县| 宁陵县| 佛冈县| 雷山县|