Overloading
A good name is worth reusing: callers should write max(...) whatever they are comparing, not memorize maxInt, maxDouble, and maxOfThree. The standard library leans on this everywhere — System.out.println is really about ten methods, one per type it can print. Java lets several methods share a name as long as their parameter lists differ. This is called overloading. The compiler picks the right one by looking at the argument types at each call site:
static int max(int a, int b) { ... } static double max(double a, double b) { ... } static int max(int a, int b, int c) { ... }
max(3, 9) calls the first, max(2.5, 1.0) the second, max(1, 7, 5) the third. Python cannot do this (a second def max replaces the first). Note: only the parameters count. Two methods that differ only in return type do not compile.
Code exercise · java
Run this overload demo and check which version handles each call.
static vs instance, first look
That a > b ? a : b is the ternary operator: condition ? value-if-true : value-if-false, Python's a if a > b else b.
Now, static. A static method belongs to the class itself: you call it directly, like Math.sqrt(2.0) or our square(6). An instance method belongs to one particular object: word.length() from lesson 2-2 needs a String object to act on. Unit 5 is where you build objects yourself.
You can finally decode lesson 1-1's full starting line, public static void main(String[] args): usable from outside the class (public), belongs to the class (static), returns nothing (void), takes an array of Strings (command-line arguments).
Quiz
Which pair can legally coexist in one class?
Code exercise · java
Your turn. Overload a method named describe three ways: describe(int n) prints `int: ` + n, describe(String s) prints `text: ` + s, describe(boolean b) prints `flag: ` + b. Call all three from main as shown.