When the Java compiler meets a call to a method name that has several overloads in a class, it follows a fixed procedure to decide which method the call invokes. Put the following stages of that procedure in the order the compiler carries them out.
1.
Search for applicable methods using only exact matches and widening conversions, with no boxing or varargs.
2.
Gather every accessible method in the class that has the name used in the call.
3.
If still no method applied, search again allowing methods with a varargs parameter.
4.
If no method applied, search again allowing boxing and unboxing between primitives and wrapper classes.
5.
Among the methods found in the first successful search, choose the most specific one or report an ambiguity error.
Unlock full course notes by signing up
Predicting the overload for six calls
A Java class declares exactly these four overloads of a static method f:
For each call in the table, type the parameter type of the overload the compiler selects: int, double, String or Object.
One label object, two selected overloads
A Java label-preview tool uses the following overloaded methods. Each returned width is measured in pixels. The Object overload assigns a fixed width, while the String overload uses the number of characters.
When preview() runs, what values are assigned to storedWidth and textWidth? Give both widths in pixels as exact integers.
Value printed by a mixed overloaded expression
Consider the following Java program. The character 'A' has Unicode value 65.
What value does the program print? Give the number to one decimal place.
Diagnose competing two-parameter overloads
A delivery application's Java test helper returns a code identifying the selected overload. Weight arguments represent grams, and distance arguments represent kilometres. The returned codes are identifiers, not calculated charges.
Consider each table call independently inside a method of RouteProbe. Assume these are the only methods named route.
An ambiguous call has applicable overloads but no unique most-specific choice. Enter ambiguous for that outcome, no match when neither overload applies, or the exact returned integer when one overload is selected.