A Java class declares exactly these four overloads of a static method f:
static void f(int x) { System.out.println("int"); } static void f(double x) { System.out.println("double"); } static void f(String s) { System.out.println("String"); } static void f(Object o) { System.out.println("Object"); }
For each call in the table, type the parameter type of the overload the compiler selects: int, double, String or Object.
Unlock full course notes by signing up
Stages of overload resolution
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.
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.