+ 3
Today's doubt is
heard that despite passing parameters to function mainly some data structure like map, set etc. we should try to pass them by reference. I mean what does it exactly mean and why & how it is useful ?
5 Answers
+ 2
When setting up a function call, pass by value loads the whole data structure into the stack frame. That takes extra time and memory in contrast with loading only a reference pointer into the stack frame.
+ 2
Davinder Kumar ah, well, in Java you do not need to decide which way to pass the argument. The data type determines it for you, as there are value types (basic numeric values) and there are reference types (strings and objects, etc).
A value type stores the data directly in its memory location. A reference type is like a bookmark. It helps you find the page in the book where the information is found. The reference does not store the information, itself. Instead, it tells where to look.
Technically, even passing a reference type is just pass by value. It is passing the value of the reference. After that, it is a matter of how the value is interpreted. The function recognizes the value as a reference type and uses it to find the data in memory. That way Java does not need to pass large amounts of data into a function.
As far as I know, Java does not give you a choice for how to pass an argument. Just pass it, and it will work efficiently.
+ 1
Davinder Kumar which language would you prefer?
0
Brian can you give a short example of this or code ?
0
Brian Java