The Plain Picture#
A named recipe you can call from other recipes: "make the sauce" is written once, and the main recipe just says "make the sauce" wherever it's needed. You hand it ingredients (tomatoes, 2 cloves of garlic) and it hands back a result (a bowl of sauce).
The Real Names#
A method is a named block of code that does one job. (Other languages call these functions.)
Parameters are the inputs it declares; arguments are the actual values you pass when calling it.
It returns a value of its return type, or nothing (
void).The first line — name, parameters, return type — is the method's signature.
static int score(int correct, boolean onTime) {
// return type int, name score, two parameters
int points = correct * POINTS_PER_CORRECT;
if (onTime) {
points += 20;
}
return points; // hand the result back; the method ends here
}
int p = score(18, true); // call it with arguments 18 and true → 200Under the Hood#
Why static for now#
A static method belongs to the class itself, so main (also static) can call it directly. Methods without static belong to
objects, which is the whole subject of the next file. Until then, write static on every method.
Each call gets its own stack frame#
From Operating Systems Ch 6: every method call pushes a stack frame holding its parameters and local variables; when it returns, the frame is popped and those variables are gone.
while score(18, true) runs: ┌──────────────────────────────┐ │ frame: score │ correct = 18, onTime = true, points = 200 ├──────────────────────────────┤ │ frame: main │ p = (waiting for score to return) └──────────────────────────────┘
This is why a variable only exists inside the block where it's declared — its scope:
for (int i = 0; i < 3; i++) {
int doubled = i * 2;
}
System.out.println(i); // compile error: i doesn't exist out here
System.out.println(doubled); // compile error: neither does doubledJava is always pass-by-value#
When you call a method, Java copies each argument's value into the parameter. For a primitive, that copy is the number itself:
static void tryChange(int x) {
x = 999;
}
int x = 10;
tryChange(x);
System.out.println(x); // 10 ← the method changed its own copy onlyFor an array (or any object), the value being copied is a reference — an address (How Computers Work Ch 7). The method gets a copy of the address, pointing at the same array:
static void tryChangeArray(int[] arr) {
arr[0] = 999;
}
int[] arr = { 1, 2 };
tryChangeArray(arr);
System.out.println(Arrays.toString(arr)); // [999, 2] ← the SAME array was changedBoth results come from one rule: Java copies the value in the variable. For primitives that's the number; for arrays and objects it's the reference. (Full picture, with diagrams, in Classes, Objects and Memory.) This is a top-5 Java interview question.
Overloading — same name, different parameters#
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
System.out.println(add(2, 3) + " " + add(2.0, 3.0)); // 5 5.0The compiler picks the version whose parameter types match the arguments. The return type alone can't distinguish two methods.
Good methods#
Do one thing, named with a verb:
score,grade,isPassing,findBest.Short — if it doesn't fit on your screen, split it.
Return values instead of printing inside them, so they can be reused and tested.
Check yourself
5 questionsParameter vs argument?
Show answerHide answer
Parameters are the named inputs in the method's definition; arguments are the actual values passed in a call.
What's a method's signature?
Show answerHide answer
Its name and parameter list (plus return type in the declaration).
After static void inc(int n) { n++; } and int a = 5; inc(a);, what is a? Why?
Show answerHide answer
5 — the method changed its own copy of the value.
After static void clear(int[] v) { v[0] = 0; } and int[] b = {7}; clear(b);, what is b[0]? Why?
Show answerHide answer
0 — the copied value is a reference to the same array, so the change is visible.
Can two methods differ only in return type?
Show answerHide answer
No — overloads must differ in their parameter lists.