In JAVA (and I guess in general OO languages), is it best to use primitive or objects as method parameters and return values?
When I say “primitive” or “objects”, I’m talking about Long or long, int or Integer, etc.
for example :
public int doStuff(int number1, int number2) {
...
}
OR
public Integer doStuff(Integer number1, Integer number2) {
...
}
OR
public Integer doStuff(int number1, int number2) {
...
}
OR
public int doStuff(Integer number1, Integer number2) {
...
}
I guess that we should be careful with objects as parameters since they can be null?
Exactly. That’s why I use the primitive type to express that the value can not be null, and the wrapper type to express that it can be null.
For instance:
or
but:
because a null velocity would wreck havoc with the physics simulation.