Passing whole object vs Passing Primitive value -
Is there a overhead or performance issue in passing whole object vs passing primitive value as in option 1 and 2?.
[ EDIT: I meant to say passing a reference of a Java object vs primitive type. From @T.J. Crowder, I understand that there is no performance issue here as the object reference size is same in both cases. But interms of API design style / perspective, which option is the best one? ]
I am at present defining the service layer. I prefer "Type 1" as I like it, but if 'Type 2' is good for performanc开发者_如何学运维e, I will go with Type 2.
Class A {
User user = SomeClass.getUser("anUser");
B b = new B();
b.doSomeOperation(user); // option 1
b.doSomeOperation(user.getUserId()); // option 2
}
Class B {
// Type 1
public void doSomeOperation(User user){
// some work done by using user.getUserId()
// I do not really need whole user object now.
}
// Type 2
public void doSomeOperation(int userId){
// some work done by userId
}
}
You never pass a "whole object" in Java. What gets passed is a reference, which is about the size of an int
or so. So what you pass — an object reference or an int
ID — has no effect on the performance in terms of the call to the function itself.
However, passing the object reference means you can act directly on the object, whereas passing the ID means that if you need to access the object, you need to look it up again by ID, which could have a negative performance impact.
Edit: Based on your update:
I understand that there is no performance issue here as the object reference size is same in both cases. But interms of API design style / perspective, which option is the best one?
That totally changes the question (and seems to drop the "performance" part of it entirely).
It totally depends on A) What you're going to do in doSomeOperation
, and B) What information the callers of doSomeOperation
are most likely to have.
If doSomeOperation
is going to need more than just the user ID, then of course pass in the object.
If doSomeOperation
doesn't need anything else other than the user ID, then you probably want to just pass in the ID. There are trade-offs. If you pass in just an int
ID, on the one hand doSomeOperation
loses its coupling to User
(which is usually good); on the other hand, doSomeOperation
's argument becomes largely meaningless. (An int
could be anything; but User
has meaning.)
If doSomeOperation
takes a User
argument but really only needs the user ID, you're placing a burden on callers of doSomeOperation
: If they just have the ID, they have to go look up the object solely for the purposes of passing it into doSomeOperation
(which is then going to ignore everything but the ID). That's clearly bad from a performance perspective.
So I think the summary is: If doSomeOperation
requires anything of User
beyond just its ID, pass in User
. If it only needs the ID and doesn't need anything else from User
, just pass in the ID.
if you are sure that you won't need any other info than just user id then go for
type 2
otherwise 1.more over you aren't passing whole object , just reference's bits will be copied.
suppose you want to update some information in
doSomeOperation
of user then you will have to fetch that object using ID that will be costly.
From the performance point of view, there is no significant difference between type1
and type2
. From the design point of view there is.
As a general rule, pass only what is needed, nothing more and nothing less.
If you pass a reference to the User
object your doSomeOperation
will be coupled to User
class. This might present you with problems in the future, such as difficulty in unit testing the method etc.
If you already have the instance of the user object pass that. When you pass the object you are only passing a ref not a chunck of memory. If you pass the id you will have to retrieve the user probably from the database. that is overhead.
精彩评论