Why did Joshua Bloch use 2*size + 1 for resizing the stack in Effective Java?
I interpret it as peace-of-mind defense against a hypothetical future bug. It's true that as written, this class won't have an array capacity of 0, so adding 1 is not strictly necessary, but that assumption could quietly fail once more features are added.
Examples of possible additional features include those from java.util.ArrayList
, which has a trimToSize()
method that can set the capacity to 0, and a constructor which allows initializing the data from a (possibly empty) collection, and a constructor that allows explicitly setting the capacity to 0. You can also imagine a feature that reduces this class's allocated capacity automatically when it is emptied. Or maybe someone will edit the DEFAULT_INITIAL_CAPACITY
constant. Now imagine that capacity-changing methods become separated by screenfuls of javadoc comments, and split across subclasses. It's easy to forget you were supposed to prevent the capacity becoming 0.
If ensureCapacity()
depends on the size being non-zero, you always have to keep that assumption in mind while you rework the class. Adding +1
is a low-cost change that removes that worry. It's also an example of a simple arithmetic way of dealing with an edge case.