Overloads chosen by argument count and type
Overloading means giving several methods the same name but different parameter lists. The compiler picks which one to call from the number and types of the arguments, at compile time, following a fixed set of rules.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write overloads max(int, int), max(int, int, int) and max(double, double), and print max(3, 9), max(4, 1, 7) and max(2.5, 1.5).
Write printAll(int), printAll(String) and printAll(int[]) that print a label and the value(s). Call each once.
Predict the output, then check: overloads g(double) and g(Object); calls g(1), g(1L), g('x') and g(Integer.valueOf(1)).
Explain it without notes
What exactly must differ between two overloaded methods? What doesn't count?
Given f(long), f(Integer) and f(int...), which one does f(5) call, and why?
Why does describe(o) call describe(Object) when o holds a String?
What is the difference between overloading and overriding?
Expected output
square: 16
rectangle: 15
circle: 3.141592653589793
hectal.in:443
localhost:8080