Reputation: 99
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)
Upvotes: 5
Views: 1432
Reputation: 4775
You're passing around an extra address using getNamePlace(String,String)
instead of getNamePlace(Person)
.
Java will not make a copy of the Person
object when passing it; it passes the reference.
Similarly, Java will not make a copy of the Strings when passing them; it passes the references.
Will you notice a significant performance difference? No.
Upvotes: 2
Reputation: 62439
When you use this:
public void getNamePlace(Person person){
\\\\work here and get the name location in the method
}
nothing actually gets passed to the method except a reference (a "pointer" in a loose sense) to the actual object of type Person
. No copying of members takes place, as you are instead instructing the method "you can find all you need at this memory location".
So technically passing a limited number of fields from person
is actually worse than passing "the entire person" (although the difference is not noticeable anyway). :)
Upvotes: 4
Reputation: 160181
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.
Upvotes: 10
Reputation: 17171
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 ;)
Upvotes: 4
Reputation: 14363
Apart from the method parameters, there are a couple of things you should tweak:
public void getNamePlace(Person person){
\\\\work here and get the name location in the method
}
to get the habits right
getNamePlace
method name when it returns nothing is not convention compliant. getXXXX
should return the value.getNamePlace
will operate on Person
so it should be defined in the class itself and then you need not to pass any parameters.Upvotes: 2
Reputation: 9283
It doesn't matter at all considering time and efficiency.
But it does matter if you want to write readable code. - It's always nice to group logically connected items, as object that they represent
for example:
writeLetterTo(person);
is much better than that:
writeLetterTo(name, surname, address, city, country);
if only all those parameters can be taken somehow from person object.
Another example:
addTwoPionts(x1, x2, y1, y2); // ugly
addTwoPoints(point1, point2); // far better
Upvotes: 2
Reputation: 12741
I personally wouldn't be too concerned with this. If you are really concerned with performance that much you probably should look at using a lower level language. As others have said, there isn't really any performance difference between the two especially when it comes to just passing variables.
If you are truly having performance issues I would suspect there are many other places in your code that are slowing you down instead of parameter passing.
Upvotes: 1
Reputation: 13196
Objects in java are passed by reference. The short answer is to just pass your objects.
The long answer is that object references are either 32 or 64 bits, depending on your java installation. A 64 bit quantity is twice as large as the JVM's typical padding size (which is often 32 bits even on 64 bit systems under some circumstances). There is a small amount of overhead associated with dereferencing the reference, but the JIT compiler will usually optimize it to a constant amount rather than a repeated hit. Furthermore, you will always have to dereference it sometime, either inside the function or outside. You might as well just save on the stack space and pass the object by reference.
Upvotes: 3