You need to pass
a number like an
int into a routine, and get back the
routine’s updated version of that value in addition to the
routine’s return value.
This often comes up in working
through strings; the routine may need to return a
boolean, say, or the number of characters
transferred, but also needs to increment an integer array or string
index in the calling class.
It is also useful in constructors, which can’t return a value but may need to indicate that they have “consumed” or processed a certain number of characters from within a string, such as when the string will be further processed in a subsequent call.
Use a specialized class such as the one presented here.
class is one of Java’s predefined
subclasses, mentioned in the Introduction
to Chapter 5. It serves as a wrapper for an
value, and also has
static methods for parsing and formatting
It’s fine as it is, but you may want something simpler.
Here is a class I wrote, called
, that is like an
Integer but specialized by omitting the overhead
Number and providing only the
incr operations, the latter overloaded to provide
a no-argument version that performs the
operator on its value, and also a one-integer version that adds that
increment into the value (analogous to the
operator). Since Java doesn’t support
operator overloading, the calling class has to call these methods instead of invoking the operations ...