Back to questions
JuniorTheoryCommon
What does the program print?
public class Main {
static void f(int x) { System.out.print("int"); }
static void f(double x) { System.out.print("double"); }
public static void main(String[] args) {
f(5);
f(5.0);
f('a');
}
}intdoubleint
- Expecting
intdoubledouble: with nof(char), the compiler takes the nearest widening, andcharwidens tointbeforedouble. - Expecting a compile error: a missing exact match is not an error when widening the argument finds a version that fits.
- Which version runs for
f(5L), and why? - Why can two methods not differ only in their result type?