Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient method parameters in Java

Tags:

java

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)

like image 422
M1 Garand Avatar asked Aug 16 '12 18:08

M1 Garand


People also ask

What are the parameters of a method in Java?

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.

How many parameters should a method have Java?

There is a technical maximum of 255 parameters that a method can have.

How do you reduce the number of parameters in a method?

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.

What are the types of parameters in class method?

Parameter Types and Values The parameter types include BOOLEAN, STRING, and INTEGER. Note that these are not InterSystems IRIS class names.


2 Answers

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.

like image 167
Dave Newton Avatar answered Oct 12 '22 17:10

Dave Newton


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 ;)

like image 34
Samuel Avatar answered Oct 12 '22 19:10

Samuel