Unit 1 · 20 min
1.9 Method Signatures
A method's signature tells you exactly how to call it.
Summary, objectives and course and exam description (CED) codes
A method is a named block of code that runs only when it is called. Its signature is the method name plus the ordered list of parameter types, and arguments in a call must match those types in number and order. Arguments are passed by call by value, and methods with the same name but different signatures are overloaded.
By the end of this session you can
- 1.9.A Identify the correct method to call based on documentation and method signatures.
- 1.9.B Describe how to call methods.
Reviewed against the CED on
1LearnThe notes, the traps and how to remember them
1. Methods and procedural abstraction
A method is a named block of code that only runs when it is called. A block is any section of code enclosed in braces { } 1.9.A.1.
Procedural abstraction allows a programmer to use a method by knowing what the method does, even without knowing how it was written 1.9.A.1. You call Math.sqrt(16.0) without ever seeing its code.
2. Parameters and method signatures
A parameter is a variable declared in the header of a method or constructor. It can be used inside the body of the method, and it lets values (arguments) be passed in and used by the method 1.9.A.2.
A method signature for a method with parameters is the method name plus the ordered list of parameter types. For a method without parameters, the signature is the method name plus an empty parameter list, for example getHeight() 1.9.A.2.
Reading a method header, part by part:
// not runnable: a method header from the Java Quick Reference
public static double pow(double base, double exponent)
| Part of the header | Meaning |
|---|---|
static |
a class method (topic 1.10) |
double (before pow) |
the return type: the type of value sent back |
pow |
the method name |
(double base, double exponent) |
the parameter list: the type and name of each parameter |
pow(double, double) |
the signature: name + ordered parameter types |
Same name, different signatures means overloaded methods (section 5).
3. Calling void and non-void methods
A void method does not have a return value, so it is not called as part of an expression 1.9.B.1. p.grow(5); is fine as a statement on its own.
// does not compile
Plant p = new Plant("Fern", 30);
int h = p.grow(5);
Real view:
error: incompatible types: void cannot be converted to int
A non-void method returns a value of the same type as the return type in its header. To use the returned value, store it in a variable or use it as part of an expression 1.9.B.2.
Plant p = new Plant("Fern", 30);
p.grow(5); // void: a statement on its own
int h = p.getHeight(); // non-void: value stored
System.out.println(h);
System.out.println(p.getHeight() * 2); // value used in an expression
Output:
35
70
4. Arguments and call by value
An argument is a value passed into a method when the method is called. Arguments must be compatible in number and order with the types in the parameter list of the method signature. An int argument can go to a double parameter, as in Math.pow(2, 10), because the int is widened (topic 1.5). Arguments are passed using call by value: each parameter is initialized with a copy of the argument 1.9.B.3. Changing the parameter inside the method does not change the caller’s variable.
public static void addTen(int value)
{
value = value + 10;
System.out.println("Inside: " + value);
}
// in main
int score = 5;
addTen(score);
System.out.println("Outside: " + score);
Output:
Inside: 15
Outside: 5
value gets a copy of 5 and becomes 15, but score in main is still 5. The copy disappears when the method ends.
5. Overloaded methods
Methods are overloaded when there are several methods with the same name but different signatures 1.9.B.4, for example abs(int x) and abs(double x). Java picks the method whose parameter types match the arguments, in order.
public static void show(int points)
{
System.out.println("Points: " + points);
}
public static void show(String player, int points)
{
System.out.println(player + ": " + points);
}
public static void show(int points, String player)
{
System.out.println(points + " for " + player);
}
// in main
show(12);
show("Ana", 12);
show(12, "Ana");
Output:
Points: 12
Ana: 12
12 for Ana
Two methods with the same name and the same parameter types do not compile, even if the parameter names differ:
// does not compile
public static int area(int side) { return side * side; }
public static int area(int length) { return length * length; }
// in main
System.out.println(area(3));
Real view:
error: method area(int) is already defined in class Main
Background (not tested): exact matches win. When one overload matches the argument types exactly, Java uses it, so
Math.abs(-4)usesabs(int)and returns theint4, whileMath.abs(-4.0)usesabs(double)and returns 4.0.
System.out.println(Math.abs(-4));
System.out.println(Math.abs(-4.0));
Output:
4
4.0
6. Flow of control in a method call
A method call interrupts the sequential execution of statements. The program first runs the statements in the method. Once the last statement in the method has run, or a return statement runs, the flow of control returns to the point right after the call 1.9.B.5.
public static void greet()
{
System.out.println(" inside greet");
}
// in main
System.out.println("before");
greet();
System.out.println("after");
Output:
before
inside greet
after
Arguments are worked out before the call, so nested calls run inside out. In Math.sqrt(Math.abs(-16)), Math.abs(-16) runs first and gives 16, then Math.sqrt(16) gives 4.0.
System.out.println(Math.sqrt(Math.abs(-16)));
Output:
4.0Lock it in: memory hooks
- SignatureSignature = name + parameter types in order. Nothing else.
- Call by valueThe method gets a photocopy of the argument; scribbling on the copy leaves the original clean.
- Nested callsInside out: the innermost call runs first.
Common misconceptions
"The return type is part of the method signature."
Not quite. The signature is only the method name and the ordered list of parameter types. The return type and the parameter names are not part of it.
"Changing a parameter inside a method changes the caller's variable."
Not quite. Call by value: the parameter gets a copy of the argument. Changing the copy does not change the caller's variable.
"You can store the result of a void method in a variable."
Not quite. A void method returns no value, so it cannot be used in an expression. int h = p.grow(5); does not compile.
"Two methods can share a name if their parameter names differ."
Not quite. Overloaded methods need different parameter types (or a different number or order of types). Same name and same parameter types do not compile, even with different parameter names.
Exam traps
- The return type and parameter names are NOT part of the signature. Only the name and the ordered parameter types are.
- Order matters: show(int, String) and show(String, int) are different signatures, so show(12, "Ana") calls only the first.
- A void method call is a statement on its own: p.grow(5); compiles, int h = p.grow(5); does not.
- Call by value: after addTen(score), score is unchanged in the caller.
2PracticeRecall it, then check yourself
In 30 seconds
- A method is a named block of code that only runs when called; a block is code inside braces.
- Procedural abstraction: use a method by knowing what it does, not how it was written.
- A parameter is a variable in a method or constructor header; the signature is the name plus the ordered parameter types.
- A void method returns nothing and is not used in an expression; a non-void method's value must be stored or used in an expression.
- Arguments must match the parameter types in number and order; call by value gives the parameters copies.
- Overloaded methods share a name but have different signatures; a call runs the method, then returns to the point right after the call.
Cover this list, say the points out loud, then take the checkpoint without scrolling back up.
Checkpoint · 4 questions
Checkpoint: Method Signatures
Pick an answer to mark it. Every answer is explained, and you can try any question again. Your saved score counts first tries only, and stays in this browser.
3PrevailLock it in for exam day
By the numbers
- Parts of a method signature two parts CED 1.9.A.2
- Kinds of method by return value two kinds CED 1.9.B.1, 1.9.B.2
- void methodreturns no value, not used in an expression
- non-void methodreturns a value of its return type
Key terms: flip the cards
Read the term, say the definition out loud, then flip to check. Struggling a little to remember is what makes it stick.
-
Method signaturetap to flip
The method name plus the ordered list of parameter types, such as pow(double, double). With no parameters: the name and an empty list.
Example and CED codes -
Parametertap to flip
A variable declared in the header of a method or constructor. It receives the value passed in and can be used in the body.
Example and CED codes -
Argumenttap to flip
A value passed into a method or constructor when it is called.
Example and CED codes -
Call by valuetap to flip
The way Java passes arguments: each parameter is initialized with a copy of the argument.
Example and CED codes -
Return typetap to flip
The type of value a method sends back, written before the method name in the header, or void if it returns nothing.
Example and CED codes -
void methodtap to flip
A method that does not return a value, so it is not called as part of an expression.
Example and CED codes -
Non-void methodtap to flip
A method that returns a value of its return type. The value must be stored in a variable or used in an expression.
Example and CED codes -
Overloadingtap to flip
Having several methods (or constructors) with the same name but different signatures.
Example and CED codes -
Procedural abstractiontap to flip
Giving a process a name (a method) so it can be used by knowing what it does, not how it does it.
Example and CED codes
Questions students ask
What is a method signature in Java?
A method signature is the method name plus the ordered list of parameter types, such as pow(double, double). A method with no parameters has the name and an empty list, such as getHeight().
What is the difference between a parameter and an argument?
A parameter is a variable declared in the method header. An argument is the value passed in when the method is called. The parameter is initialized with a copy of the argument.
What does call by value mean?
Each parameter is initialized with a copy of the argument. Changing the parameter inside the method does not change the caller's variable.
What are overloaded methods?
Overloaded methods have the same name but different signatures, such as abs(int x) and abs(double x). Java chooses the one whose parameter types match the arguments.
Can you call a void method inside an expression?
No. A void method returns no value, so it is called as a statement on its own, such as p.grow(5);.