Question

Let me see if I understand this. Due to Java's "pass-by-the-value-of-the-reference” nature it is much less costly to pass around an array or large collection of objects (say bitmaps) than it could be... Because you're actually passing a high level reference variable... How far off am I?

Was it helpful?

Solution

"pass-by-the-value-of-the-reference" has it summed up, where the value of the reference [type] is an internal opaque value:

The reference type of the Java virtual machine is cleverly named reference. Values of type reference come in three flavors: the class type, the interface type, and the array type. All three types have values that are references to dynamically created objects.

..

The basic unit of size for data values in the Java virtual machine is the word--a fixed size chosen by the designer of each Java virtual machine implementation. The word size must be large enough to hold a value of type byte, short, int, char, float, returnAddress, or reference.


Notes:

  1. Android-Dalvik is not Java-JVM. However, for sake of discussion it takes "equivalent stack space" to pass a reference value and an integer value or utilize a local variable of the same.

  2. It's not all about the size. Where possible, passing and using a long is better than a Long, even though the reference value might actually be "smaller".

    Trivially, there is no indirect lookup or boxing/unboxing and associated object required for the long value. (Examples of additional memory locality benefits is better in .NET which supports larger "primitive/struct" values and doesn't need to box/unbox.)

  3. The phrase "actually passing a high level reference variable" is wrong because variables are not passed in Java. Only values are passed as arguments: for reference-typed expressions, including simple variable expressions, the reference value is passed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top