The compiler distinguishes overloaded methods by their signature—a combination of the method’s name and the number, types and order of its parameters. If the compiler looked only at method names during compilation, the code in Fig. D.7 would be ambiguous—the compiler would not know how to distinguish between the two square
methods (lines 14–19 and 22–27). Internally, the compiler uses longer method names that include the original method name, the types of each parameter and the exact order of the parameters to determine whether the methods in a class are unique in that class.
For example, in Fig. D.7, the compiler might use the logical name “square
of int
” for the square
method that specifies an int
parameter and “square
of double
” for the square
method that specifies a double
parameter (the actual names the compiler uses are messier). If method1
’s declaration begins as
void method1( int a, float b )
then the compiler might use the logical name “method1
of int
and float
.” If the parameters are specified as
void method1( float a, int b )
then the compiler might use the logical name “method1
of float
and int
.” The order of the parameter types is important—the compiler considers the preceding two method1
headers to be distinct.