I am still a beginner to java and I have a question on an efficient way to pass in parameters. When passing in objects in a method is it more efficient to pass in portions of an object or does it not matter at all? for example, lets say I have a Person object which has several attributes (name, height, gender, location, hairColor etc.) and I have a method which needs to work on two of these attributes. When I pass the info into the method, does it save me memory, process time etc to only pass in the needed info (like name and location) or is it the same as just passing in the whole person object?
is:
public void getNamePlace(String name, String location){
\\\\work here
}
any different efficiency-wise than:
public void getNamePlace(Person person){
\\\\work here and get the name location in the method
}
Thanks a lot for the help. I'm sorry if it is a dumb question but I'm trying to figure out the best mentality to approach this. Thank you.
p.s. I am thinking more on the method call itself. How does it work. In isolation, is:
public void getNamePlace(String name, String Location) --> More bits passed around and memory used than --> public void getNamePlace(Person person)
Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
There is a technical maximum of 255 parameters that a method can have.
Well, if a method has five or more parameters, then you should consider refactoring that method and decreasing the number of parameters. This is where Introduce Parameter Object refactoring comes into play. In short, this refactoring is a way of replacing a method's argument list with a single parameter object.
Parameter Types and Values The parameter types include BOOLEAN, STRING, and INTEGER. Note that these are not InterSystems IRIS class names.
You're passing reference values. The more values you pass, the more work is being done.
Is it a measurable difference? Technically, yes–but miniscule in the extreme (e.g., another parameter is another bytecode, and that's all. Vanishingly small difference in performance and class size.)
Is it an appreciable difference? No.
The mentality here is that premature optimization, particularly at this level, isn't something to deal with.
Others have pointed out there are readability and data issues that are more important.
It "doesn't matter at all". In Java you only pass the reference of the object (as a value) to the function. It's a very small data type and it tells the VM where the object is located in memory.
Take a look at Is Java "pass-by-reference" or "pass-by-value"? for a very good answer ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With