Methods
Return type, name, parameters, body:
Arguments are copies
Java is always pass-by-value. For objects the reference is copied — you can mutate the object, but not reassign the caller’s variable.
Overloading
Same name, different parameter lists:
A different return type alone is not enough.
Varargs
Must be the last parameter.
Scope
A variable lives inside its block:
static methods belong to the class, not an instance:
Writing good methods
- one job per method, and the name says it — if you need “and”, split it
- verbs first:
computeDiscount,loadCustomer,isValid - more than three parameters? use a record instead
- return early instead of nesting
Recursion
Java has no tail-call optimisation — deep recursion throws StackOverflowError.
★ Exercises
isPalindrome(String)— ignore case.- Overload
maxthree ways: twoint, twodouble, any number ofint. countVowels(String).- Write Fibonacci recursively and iteratively; time both for
n = 40withSystem.nanoTime(). formatName(String first, String last)→"Miller, Ann", tolerating stray spaces and mixed case.