Command Palette

Search for a command to run...

Back to the lesson: Topic 3.4 — Method Overloading
Core Java · Example 2 of 3

The three phases: widening, then boxing, then varargs

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.

01

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).

02

Write printAll(int), printAll(String) and printAll(int[]) that print a label and the value(s). Call each once.

03

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

01

What exactly must differ between two overloaded methods? What doesn't count?

02

Given f(long), f(Integer) and f(int...), which one does f(5) call, and why?

03

Why does describe(o) call describe(Object) when o holds a String?

04

What is the difference between overloading and overriding?

The three phases: widening, then boxing, then varargs
Sign in to run this example in your browser.

Expected output

f(long)
f(long)
f(long)
f(Integer)
f(Object)
f(Object)
f(int...) with 0 args
f(int...) with 2 args