Java Reference
In-Depth Information
return
return
total
;
}
Polymorphism is a great boon for software maintenance: if a new subclass is added, the code
in the main program does not change. Further, all the code that is specific to, say, polygon
handling, is all in one place: in the source file for the
Polygon
class. This is a big improve-
ment over older languages, where type fields in a structure or record were used with case or
switch statements scattered all across the software. Java makes software more reliable and
maintainable with the use of polymorphism.
Passing Values
Problem
You need to pass a number like an
int
into a routine and get back the routine's updated ver-
sion 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.
Solution
Use a specialized class such as the
MutableInteger
class presented here.
Discussion
The
Integer
class is one of Java's predefined
Number
subclasses, mentioned in the Introduc-
parsing and formatting integers.
It's fine as it is, but you may want something simpler.