Java Reference
In-Depth Information
Consider the following declaration for a method squareDivide() :
int squareDivide(int x, int y) {
int z = x * x/y * y;
return z;
}
Consider the following snippet of code that calls the squareDivide() method:
squareDivide((4+4), (2+2));
You can visualize the execution of the above call as if you have written the squareDivide() method as follows.
Note that the actual argument expressions of (2+2) and (4+4) are evaluated multiple times inside the method's body.
int squareDivide() {
int z = (4+4)*(4+4)/(2+2)*(2+2);
return z;
}
Pass by Need
Pass by need is similar to pass by name with one difference. In pass by name, actual parameters are evaluated each
time they are used in the method. In pass by need, the actual parameters are evaluated only once upon their first use.
When a thunk for an actual parameter is called for the first time, it evaluates the actual parameter expression, caches
the value and returns it. When the same thunk is called again, it simply returns the cached value, rather than
re-evaluating the actual parameter expression again.
Parameter Passing Mechanisms in Java
Java supports two kinds of data types: primitive data type and reference data type. A primitive data type is a simple
data structure and it has only one value associated with it. A reference data type is a complex data structure and it
represents an object. A variable of a primitive data type stores the value directly at its memory address. Suppose you
have an int variable id . Further suppose it has been assigned a value of 754 and its memory address is 131072 .
int id = 754;
Figure 6-11 shows the memory state of the id variable.
131072
id
754
Variable name
Value stored at the address
Memory address
Figure 6-11. The memory state for an id variable when its value is 754
 
Search WWH ::




Custom Search