A method's signature is its name plus its parameter types, and Java uses it to decide which method a call runs. A non-void call evaluates to the method's return value; a void call gives back nothing. Static methods are called on the class name, as in Math.abs(-6), and instance methods on an object, as in word.length().
Quick check
After String word = "plum"; what does the variable word store?Show answerHide answer
A reference to a
Stringobject holding "plum", not the characters themselves.A header reads public static double half(int n). What type of value does a call to half give back?Show answerHide answer
A
double. The type written just before the method name is the return type.
Know these already? Jump to the practice questions
What you'll be able to do
- Identify the parts of a method header and state its signature.
- Explain what void and non-void calls evaluate to.
- Determine which overloaded method a call runs.
- Decide whether to call a method on its class name or on an object.
- Interpret preconditions and postconditions to judge a call.
Key terms
Method signature
A method's name with its parameter types, in order. The return type is not part of it.
- In plain English
- The name and the kind of input it expects.
- Don't write
- "The signature of
double half(int n)isdouble half."
Parameter
A variable declared in a method header that receives a value when the method is called.
- In plain English
- A slot the call fills.
- Don't write
- "In
half(7), 7 is the parameter."
Argument
A value passed to a method in a call, copied into the matching parameter.
- In plain English
- What you put in the slot.
- Don't write
- "Arguments are matched to parameters by name."
Overloaded methods
Methods in one class with the same name and different parameter lists.
- In plain English
- One name, several versions, told apart by their inputs.
- Don't write
- "Two methods can be overloaded by changing only the return type."
What does a method header tell you?
The header is the first line of a method:
public static double half(int n)
{
return n / 2.0;
}
| Part | Here | Meaning |
|---|---|---|
static | present | call it on the class name |
| Return type | double | the type a call gives back |
| Name | half | what you write to call it |
| Parameters | (int n) | the call passes one int |
The signature is half(int). A static method is called on its class name; inside its own class the name alone works: half(7). A method without static is an instance method: it works on one object, so you call it on a reference to that object.
Header rules are Java syntax, not on the Java Quick Reference: Learn this.
Key idea
A signature is the name plus the parameter types in order; parameter names and the return type are not part of it.
Check yourself
Show answerHide answer
The signature is countVowels(String) and the return type is int.
What does a call evaluate to?
A call to a non-void method is an expression: Java runs the method, and the call takes the value it returns. Using half:
double h = half(7);
double s = half(4) + 1;
half(9);
h is 3.5, and s is 2.0 + 1, which is 3.0. The third line runs the method, but 4.5 is thrown away because nothing stores or uses it.
A void method returns no value, so its call can only stand alone as a statement, such as System.out.println("hi");. int x = System.out.println("hi"); does not compile: there is no value to store.
Check yourself
Show answerHide answer
No. greet is void, so the call has no value to store in s.
How are arguments matched to parameters?
Java matches arguments to parameters by position: first to first, second to second. A call compiles only if:
- it has as many arguments as there are parameters;
- each argument's type fits its parameter. An
intargument can go to adoubleparameter (4 is widened to 4.0), but adoubleargument for anintparameter does not compile.
Names play no part. For percent(int part, int whole), the call percent(whole, part) compiles but sends the caller's whole into part.
Each parameter starts as a copy of its argument, so assigning to an int parameter leaves the caller's variable unchanged.
Check yourself
Show answerHide answer
Only area(3, 4). area(3) has too few arguments, and 4.5 is a double sent to an int parameter.
How does Java choose between overloaded methods?
For overloaded methods, Java runs the version whose parameter list matches the call's arguments in number, order and type. Suppose a class has int fee(int hours), int fee(int hours, int rate) and double fee(double hours). fee(2, 5) runs the second version and fee(4.0) the third. fee(4) runs the first: an int argument matches fee(int) exactly, so Java picks it instead of widening 4 to 4.0.
Adding double fee(int h) would not compile: its signature, fee(int), matches the first version, and a different return type does not make a different method.
Check yourself
Show answerHide answer
print(String), because "7" in quotes is a String.
What do preconditions and postconditions promise?
A comment above a method says how to use it. A precondition must be true when the method is called. The method does not check it: that is the caller's job, and if it is false the result is not guaranteed. A postcondition is true when the method finishes, such as what it returns.
/** Precondition: items >= 0 and perBox > 0
* Postcondition: returns the fewest boxes that hold
* all the items
*/
public static int boxesNeeded(int items, int perBox)
{
return (items + perBox - 1) / perBox;
}
The postcondition lets you predict boxesNeeded(12, 5) without tracing: 3 boxes. The precondition rules out boxesNeeded(12, 0), which would divide by zero.
Check yourself
Show answerHide answer
No. items must be at least 0, so the result of this call is not guaranteed.
Worked example 1: Trace void and non-void calls
Question: What is printed when the code in main runs?
public static int twice(int n)
{
return n * 2;
}
public static void show(int n)
{
System.out.println(n);
}
// in main:
int a = twice(4);
twice(a);
show(a);
a = twice(a) + 1;
show(a);
What tells you the method
Void and non-void calls are mixed, so trace each call and track a.
Step 1: Trace each statement in
main.Statement Call returns aPrinted int a = twice(4);8 8 twice(a);16 8 show(a);nothing 8 8 a = twice(a) + 1;16 17 show(a);nothing 17 17 WhyThe 16 from
twice(a);is never stored, soastays 8 until the fourth statement.Step 2: Read the printed column.
Each value is on its own line.
Whyshowusesprintln, which ends the line.
Check it
The fourth statement uses a as 8, so the result is 8 × 2 + 1 = 17, which matches the table.
Answer
8 then 17 on the next line.
Worked example 2: Choose the overloaded version
Question: What is printed?
public static int fee(int hours)
{
return hours * 3;
}
public static int fee(int hours, int rate)
{
return hours * rate;
}
public static double fee(double hours)
{
return hours * 2.5;
}
// in main:
System.out.println(fee(4) + fee(2, 5));
System.out.println(fee(4.0));
What tells you the method
Three methods share one name, so match each call to a signature first.
Step 1: Match each call.
fee(4)runsfee(int),fee(2, 5)runsfee(int, int)andfee(4.0)runsfee(double).WhyJava picks the version whose parameter types match the arguments.
Step 2: Evaluate.
fee(4)is 12 andfee(2, 5)is 10, so the first line prints 22.fee(4.0)is 10.0, adouble.WhyBoth calls on the first line return
int, so+adds them; adoubleprints with a decimal point.
Check it
Had fee(4) run fee(double), the first line would print 20.0; it prints 22 because fee(int) matches exactly.
Answer
22 then 10.0 on the next line.
Worked example 3: Use the documentation to pick a call
Question: The method boxesNeeded (in the section on preconditions) is in a class named Packer. Which statement, in another class, stores in b the boxes needed for 23 items, 5 per box?
- A)
int b = Packer.boxesNeeded(5, 23); - B)
int b = Packer.boxesNeeded(23.0, 5); - C)
int b = boxesNeeded(23, 5); - D)
int b = Packer.boxesNeeded(23, 5);
What tells you the method
A "which call" question, so test each option against the header.
Step 1: Read the header.
The method is static, so call it on
Packer; it takesitemsthenperBox, bothint.WhyThe header fixes how a call must look.
Step 2: Test each option.
A swaps the order. B sends a
doubleto anintparameter, and C leaves outPacker., so neither compiles. D matches.WhyOnly D has the class name, the order and the types right.
Step 3: Trace D.
(23 + 5 - 1) / 5 is 27 / 5, which is 5 with
intdivision.WhyTracing confirms the call meets the postcondition.
Check it
5 boxes hold 25 items and 4 hold only 20, so 5 is the fewest. Option A would return 27 / 23, which is 1.
Answer
D
Your turn
Question: What is printed?
public static int bump(int n)
{
n = n + 10;
return n;
}
// in main:
int x = 5;
int y = bump(x);
System.out.println(x + " " + y);
Step 1: Start the call.
bump(x)copies 5 into the parametern.Step 2: Your go: trace the body of
bump, then decide whatxandyhold.
Show the full solutionHide the solution
Step 2. n becomes 15 and the method returns 15, so y is 15. Only the copy n changed, so x is still 5. The println joins 5, a space and 15.
Answer: 5 15
Traps
Trap
Two methods can share a name and parameter types if their return types differ.
It seems right because the return type sits next to the name. But the signature is only the name and parameter types, so int size(String s) and double size(String s) cannot be in one class.
Can int f(int a) and int f(double a) be in the same class?
Yes. Their signatures, f(int) and f(double), differ, so f is overloaded.
Trap
An argument goes to the parameter with the same name.
It seems right because code often names them alike. But Java matches by position: the first argument goes to the first parameter, whatever the names.
For percent(int part, int whole), which parameter receives whole in the call percent(whole, part)?
part, because whole is the first argument.
What's examined
On the AP® Computer Science A exam (Unit 1), multiple-choice questions ask what a call returns or prints, which call compiles or which overloaded version runs. Free-response questions give headers and documentation that your code must call correctly.
Practice questions
- Recall What is the signature of
public static double average(int a, int b)?
- A)
double average(int a, int b) - B)
average(int, int) - C)
average(a, b) - D)
public static average
Show answer
Answer: B. A signature is the name plus the parameter types in order.
Why the others are wrong:
- A includes the return type, which is not part of the signature.
- C lists parameter names, not types.
- D gives modifiers and no parameter types.
- Apply What is printed?
public class Counter
{
private int count = 0;
public void click()
{
count++;
}
public int getCount()
{
return count;
}
}
// in main:
Counter c = new Counter();
c.click();
c.click();
int n = c.getCount();
c.click();
System.out.println(n + " " + c.getCount());
Show answer
Answer: 2 3
Marking points:
nstores the return value 2, taken after two clicks.- The third click makes
count3, butnkeeps the 2 it already stored. c.getCount()is called again in theprintln, so it returns 3.
- Apply What is printed?
public static String tag(int n)
{
return "int";
}
public static String tag(double d)
{
return "double";
}
public static String tag(int n, int m)
{
return "pair";
}
// in main:
System.out.println(tag(7) + tag(7.0) + tag(7, 0));
Show answer
Answer: intdoublepair
Marking points:
tag(7)has oneint, so it runstag(int).tag(7.0)has onedouble, andtag(7, 0)has twointarguments.- The three strings are joined with no spaces.
- Exam-style The code segment should print what percent 15 is of 60, which is 25.0. Give the number of the line that stops it working.
public static double percent(int part, int whole)
{
return 100.0 * part / whole;
}
1: int scored = 15;
2: int possible = 60;
3: double p = percent(possible, scored);
4: System.out.println(p);
Show answer
Answer: Line 3.
Marking points:
- Arguments match parameters by position, so
partgets 60 andwholegets 15, and the segment prints 400.0. - The fix is
double p = percent(scored, possible);, which prints 25.0.
- Apply A class has
public static void report(int n)andpublic static int square(int n). Which statement, in the same class, does not compile?
- A)
report(square(3)); - B)
square(3); - C)
int r = report(3); - D)
double d = square(3);
Show answer
Answer: C. report is void, so the call has no value to store in r.
Why the others are wrong:
- A compiles:
square(3)is anint, which is a valid argument forreport. - B compiles: a return value may be ignored.
- D compiles: the
int9 is widened to 9.0.
- Exam-style A method is called as
String s = Name.initials("Ada", "Lovelace");from another class. Which header fits this call?
- A)
public static String initials(String first, String last) - B)
public static void initials(String first, String last) - C)
public String initials(String first, String last) - D)
public static String initials(String name)
Show answer
Answer: A. It is static, returns a String and takes two String parameters.
Why the others are wrong:
- B is void, so the call has no value to store in
s. - C is an instance method, so it cannot be called on the class name
Name. - D takes one parameter, but the call passes two arguments.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- The header gives the return type, the name and the parameter list; the signature is the name and parameter types only.
- A non-void call takes the value it returns; a void call is a statement with no value.
- Arguments go to parameters by position, and each parameter starts as a copy.
- Overloaded versions differ in their parameter lists, and Java runs the one the arguments match.
- Static methods are called on the class name, instance methods on an object.