UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.9
1.9 Method Signatures
A method's signature is how Java knows which method you mean. Reading a signature tells you how to call it and what you get back.
What you need to know
- A method header looks like:
public static int max(int a, int b)— access modifier, (optionally)static, return type, name, parameter list. - The signature is the method name plus the parameter types, in order:
max(int, int). The return type is not part of the signature. - Parameters are the variables in the header. Arguments are the values you pass in a call. They must match in number, order, and (compatible) type.
- Overloading: a class can have several methods with the same name if their parameter lists differ. Java picks the one whose parameters match the arguments.
- A method with return type
voidreturns nothing and is called as a statement. A method with a non-void return type produces a value you can use in an expression. - Passing an int where a double parameter is expected works (widening). Passing a double where an int is expected is a compile error.
Worked example
public static double area(double radius) // area(double)
public static double area(double w, double h) // area(double, double)
public static void greet(String name) // greet(String)
area(3); // calls first: 3 widens to 3.0
area(4, 5); // calls second
greet("Ram"); // fine; returns nothing
int x = greet("Ram"); // compile error: void has no value
Exam tip: To decide which overloaded method a call uses, count the arguments and match their types. When an FRQ gives you a method header to implement, copy it exactly — changing the return type or a parameter name costs points.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- A full method header:
[access] [static] returnType name(paramType1 name1, paramType2 name2). Access ispublicorprivate.staticis optional. Return type is any type orvoid. - The signature is name + parameter types in order.
calc(int, double)andcalc(double, int)are different signatures — both can coexist. Return type and parameter names are not part of the signature, soint f(int x)anddouble f(int y)conflict. - Overload resolution: Java picks the method whose parameter types match the argument types exactly; if none match exactly, it picks the closest via widening.
f(5)with onlyf(double)defined works (5 widens).f(5.0)with onlyf(int)defined fails. - Arguments are evaluated before the call, left to right.
f(x, x++)is out of scope, butf(a + 1, b * 2)computes both, then calls. - The number of arguments must equal the number of parameters. No defaults, no optional parameters in the Java the exam covers.
- On FRQs, you're given the header to implement. Copy it character for character. Changing
publictoprivate, droppingstatic, renaming a parameter — each can cost the rubric point for the header.
Mistakes that cost points
- Passing a double where an int parameter is expected. Compile error. Narrowing doesn't happen automatically.
- Assuming return type distinguishes overloads. It doesn't. Two methods with the same name and parameter types won't compile regardless of return type.
- Rewriting the FRQ header. Use exactly what's given.
- Assigning a void call to a variable.
int x = printer.show();where show is void — compile error.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Given the header
public static int compute(int x, double y), which call will compile?Show answer
Answer: B. The int 3 widens to double for y. Option A tries to pass a double for the int parameter, which doesn't compile.
Q2 Which of the following is part of a method's signature?
Show answer
Answer: B. Signature = name + parameter types. Return type and parameter names aren't included.
Key vocabulary
- Method signature
- the method name and its parameter types in order
- Return type
- the type of value a method produces, or void
- Overloading
- multiple methods with the same name but different parameter lists
- Parameter
- a variable in a method header
- Argument
- a value passed to a method when it is called