Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 performance, 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
    }
}
like image 441
srivaradhan Avatar asked Mar 09 '11 06:03

srivaradhan


People also ask

What is the difference between passing primitive data types versus passing objects?

When you pass a primitive, you actually pass a copy of value of that variable. This means changes done in the called method will not reflect in original variable. When you pass an object you don't pass a copy, you pass a copy of 'handle' of that object by which you can access it and can change it.

Are all primitive data types passed by value?

Primitive data types such as string, number, null, undefined, and boolean, are passed by value while non-primitive data types such as objects, arrays, and functions are passed by reference in Javascript.

What is the difference between pass by value and pass by reference in Java?

Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that's why it's called pass by value. Pass by Reference: An alias or reference to the actual parameter is passed to the method, that's why it's called pass by reference.

How does Java pass primitives?

Java doesn't pass objects. It passes object references - so if anyone asks how does Java pass objects, the answer is: "it doesn't". In the case of primitive types, once passed, they get allocated a new space in the stack and thus all further operations on that reference are linked to the new memory location.


2 Answers

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.

like image 101
T.J. Crowder Avatar answered Oct 05 '22 17:10

T.J. Crowder


  • 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.

like image 34
jmj Avatar answered Oct 05 '22 19:10

jmj