java中的值传递

C++ NOTE: Inheritance is similar in Java and C++. Java uses the extends keyword instead of

the : token. All inheritance in Java is public inheritance; there is no analog to the C++ features

of private and protected inheritance.

 

 

NOTE: Some people think of super as being analogous to the this reference. However, that

analogy is not quite accuratesuper is not a reference to an object. For example, you cannot

assign the value super to another object variable. Instead, super is a special keyword that

directs the compiler to invoke the superclass method.

 

 

Let us review the computer science terms that describe how parameters can be passed to a

method (or a function) in a programming language. The term call by value means that the

method gets just the value that the caller provides. In contrast, call by reference means that

the method gets the location of the variable that the caller provides. Thus, a method can modify

the value stored in a variable that is passed by reference but not in one that is passed by

value. These call by . . . terms are standard computer science terminology that describe

the behavior of method parameters in various programming languages, not just Java. (In

fact, there is also a call by name that is mainly of historical interest, being employed in the

Algol programming language, one of the oldest high-level languages.)

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.

 

 

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;

}

Lets 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 tripledit is now 30. But percent is still 10 (see Figure 46).

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

Figure

 

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);

}

percent =

x =

value copied

value tripled

10

30

Chapter 4. Objects and Classes

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.

 

你可能感兴趣的:(java,object,Parameters,reference,inheritance,Primitive)