Assume that we have a given interface:
public interface StateKeeper {
public abstract void negateWithoutCheck();
public abstract void negateWithCheck();
}
and following implementations:
class StateKeeperForPrimitives implements StateKeeper {
private boolean b = true;
public void negateWithCheck() {
if (b == true) {
this.b = false;
}
}
public void negateWithoutCheck() {
this.b = false;
}
}
class StateKeeperForObjects implements StateKeeper {
private Boolean b = true;
@Override
public void negateWithCheck() {
if (b == true) {
this.b = false;
}
}
@Override
public void negateWithoutCheck() {
this.b = false;
}
}
Moreover assume that methods negate*Check()
can be called 1+ many times and it is hard to say what is the upper bound of the number of calls.
The question is which method in both implementations is 'better' according to execution speed, garbage collection, memory allocation, etc. -
negateWithCheck
ornegateWithoutCheck
?Does the answer depend on which from the two proposed implementations we use or it doesn't matter?
Does the answer depend on the estimated number of calls? For what count of number is better to use one or first method?