0

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?

user207421
  • 305,947
  • 44
  • 307
  • 483
limeandcoconut
  • 415
  • 6
  • 21
  • Well I would say low-level... But yes, all you're doing is passing in a bit-pattern that refers to the location in memory where your object is stored. – Azar Apr 13 '14 at 23:18
  • 4
    A little imprecise, but essentially correct. – Hot Licks Apr 13 '14 at 23:19
  • Also consider `List`; [profile](http://stackoverflow.com/q/2064427/230513) to see more. – trashgod Apr 13 '14 at 23:21
  • Java is always pass by value. However, the value of an Object type is its' reference address. See also the default `toString` implementation of any Object type. – Elliott Frisch Apr 13 '14 at 23:21
  • You can pass *anything* of type object and you won't see more overhead. That's the meaning of your statement. You don't have to ask the question again for every different class. Asking just shows that you don't understand what you've said. – user207421 Apr 13 '14 at 23:47
  • That's a fair criticism. I removed the duplicate question – limeandcoconut Apr 14 '14 at 00:54
  • @trashgod Any suggestions for a profiler to work with Android Studio? I'm just starting with profiling. – limeandcoconut Apr 14 '14 at 04:08

1 Answers1

1

"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.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • (3) doesn't make sense. Variables of object type are references and they are passed by value. – user207421 Apr 13 '14 at 23:49
  • @EJP I am trying to draw a distinction between the concepts of a variable which "references" an object (by indirection through a value) and the "reference value" which is passed as an argument. The wording could probably be different. – user2864740 Apr 13 '14 at 23:55
  • What distinction? A reference variable contains a reference value. What do you need a distinction for? An int variable contains an int value. The parallel is exact. You're just creating confusion. I would delete (3). – user207421 Apr 13 '14 at 23:59
  • @EJP I've changed the wording some and hope that my intent is more clear. – user2864740 Apr 14 '14 at 00:14