0

When I call a function are the parameters used to call stored in the memory of caller function?

If I make a call to funcA like below, will the evaluated value of funcB(params) stored inside the memory of main(). Is storing the value of funcB(params) in a variable and then passing it to funcA more efficient?

main() {

funcA(funcB(params))

}
Mayank
  • 17
  • 4

1 Answers1

1

It doesn't make sense to talk about 'memory of the caller' in Java. All the object instances reside in the same shared memory pool called Heap in the Java Virtual Machine. Memory does not belong to a specific method in Java. By the way, just to be clear on the terminology, funcA and funcA are most likely methods of some Class rather than functions.

With respect to assigning to a variable, you have to understand that in Java there are two ways of passing arguments:

  • Primitive types are passed by value always
  • Objects are passed by reference always

Assigning an object to a variable is just assigning a reference to the same object. You should assign to a variable only if it makes the code more clear, not because of memory concerns.

aled
  • 21,330
  • 3
  • 27
  • 34
  • Java is always pass by value. But, for Objects, the value is a copy of the reference. See https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value/49330809 – Old Dog Programmer Aug 22 '23 at 21:49