• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simulating pass by reference for primitives  RSS feed

 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since Java always passes arguments to methos by-value, often we simulate pass-by-reference by wrapping the primitive in an array or an object. Since a copy of the reference is passed to the method, the caller can then inspect the changed values soon after the called method returns.
Now for the question. Is there a reason to believe one approach is better than another - using an array as a wrapper Vs. using a custom class object as a wrapper - in terms of performance issues.




------------------

Ajith Kallambella M.
Sun Certified Programmer for the Java2 Platform.
 
Mirko Froehlich
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt that there will be a significant (if at all) performance difference between using arrays or wrapper objects, so either way should work fine. Personally, I think that using wrapper objects is cleaner and more readable. In your specific case you are dealing with int values, and you would probably want to use the Integer wrapper class instead of MyCustomClass.
-Mirko
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will not be able to use the Integer class, or for that matter any Java wrapper classes because all of them are immutable.
My specific concern was the performance overhead involved in instantiating MyCustomClass when compared with instantiating ( a lightweight??) array object. Instantiation also involves class loading etc.

------------------

Ajith Kallambella M.
Sun Certified Programmer for the Java2 Platform.
 
Kartik Shah
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to me instantiation of the object will take more resources rather than instantiation of array.
However instantiation is not only the point. If we are using array we will use array index based approach to modify the value. However in the object based approach we will use either method or direct access to member variable(which is not so good). It is this what we should worry about it most. Because operation of assignment is performed numerous time if we are calling this function in loop.
 
Kartik Shah
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or so to say I would rather use in following way
int changeValue(int i)
{
return (i + 100);
}
and use in the calling program
value = changeValue(value);
Basically it all comes back to what your method is contracted to do.
Here what we want changeValue is contracted to change the value. And it is will do some operation( in this case addition), completes it work and returns back the changed value which we want it to do.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!