-4

I couldn't find any best practice guide line or question about this.

public void method(int primitiveVar)
{

}

vs

public void method(CustomObject objectVar)
{
    // am I slower or do I generate any problem at all?
}

Is there any performance improvement by using one over the other by the time you call the method?

Thanks

Oscar Ortiz
  • 813
  • 8
  • 16
  • I'm getting down votes to my question yet I don't know why, and people is also clearly answering different answers so why is this not a well formed question? Should I also add that I know about early micro optimization and that I know the more body you add to a method the more you processing make? – Oscar Ortiz Jun 25 '13 at 16:49
  • 1
    This is not a well formed question because it's not constructive; generally this sort of difference is not significant (provable by how few optimizations are centered on this sort of code fix). Therefore responses are liable to be mostly opinions. Finally, the fact that Java does not treat primitives as objects has long been a point of weakness of the language; so using an object is hardly out of line with best practices. – Nathaniel Ford Jun 25 '13 at 16:59
  • @NathanielFord Thank you for the reply; You just answered the original question; I'm sorry but I didn't know the last thing you said, if I knew, I wouldn't be asking this. But are the questions only for the GURUs? – Oscar Ortiz Jun 25 '13 at 17:02
  • Not sure what you mean by 'GURUs'? The main thing is that S.O. tries to shy away from this sort of vague optimization theory, and concentrate on concrete problems. This is not a concrete problem because there isn't a provided use case where there may be any sort of observable difference. – Nathaniel Ford Jun 25 '13 at 17:10

3 Answers3

3

Much like the other answers say it most likely won't have and impact on performance with whatever you do. But if you are planning on passing large amounts of primitives, I'd just pass and object to have more efficient easy to read code.

gcalex5
  • 1,091
  • 2
  • 13
  • 23
2

Objects are passed by value, so that means arguments are copied and then passed to the method being executed. So if you pass a reference, which is what you do when you pass on object, that reference gets copied. It should be the same speed as passing a primitive but I can see in theory how on some custom hardware it might be slower if a reference to an object uses more words than some primitive type.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0

It doesn't really matter. But, if still that is the question, then sharing through primitives has advantage sometimes (depending on how you have written and what you are doing), as references are actually unboxed to primitives.

Vikas V
  • 3,176
  • 2
  • 37
  • 60