Method Invocation Conversions Involving References – Object-Oriented Programming

5.10 Method Invocation Conversions Involving References

The conversions for reference value assignment are also applicable to method invocation conversions, except for the narrowing conversion for constant expressions of non-long integer type (Table 2.17, p. 47). This is reasonable, as parameters in Java are passed by value (§3.10, p. 127), requiring that values of the actual parameters must be assignable to formal parameters of the compatible types.

In Example 5.18, the method sendParams() at (17) has the following signature, showing the types of the formal parameters:

Click here to view code image

sendParams(Object, Stack, IStack, Stack[], IStack[])

The method call at (15) has the following signature, showing the types of the actual parameters:

Click here to view code image

sendParams(Stack, SafeStack, IStack, SafeStack[], ISafeStack[]);   // Compile time

At runtime, the actual type of the objects whose reference values are passed at (15) is the following:

Click here to view code image

sendParams(SafeStack, SafeStack, SafeStack, SafeStack[], SafeStack[]);  // Runtime

Note that the assignment of the values of the actual parameters to the corresponding formal parameters is legal, according to the rules for assignment discussed earlier. The method call at (16) provides another example of the parameter-passing conversion. It has the following signature:

Click here to view code image

sendParams(ISafeStack[], Stack, ISafeStack, Stack[], SafeStack[]); // Compile time

At runtime, the actual type of the objects whose reference values are passed at (16) is the following:

Click here to view code image

sendParams(SafeStack[], SafeStack, SafeStack, SafeStack[], SafeStack[]);// Runtime

Analogous to assignment, the rules for parameter-passing conversions are based on the reference type of the parameters and are enforced at compile time. It is instructive to compare the type of a formal parameter with the type of its corresponding actual parameter at compile time, and the type of the object whose reference value is actually passed at runtime. The output in Example 5.18 shows the class of the actual objects referenced by the formal parameters at runtime, which in this case turns out to be either SafeStack or SafeStack[]. The characters [L in the output indicate a one-dimensional array of a class or interface type (see the Class.getName() method in the Java SE Platform API documentation).


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *