Skip to main content

Unit 1 · Topic 1.9

1.9 Method Signatures

Methods let you name a chunk of code and run it whenever you need it. To call a method correctly you need its signature: its name and the types of its parameters, in order. This topic also covers what happens to the flow of a program when a method is called.

Key terms

  • method
  • parameter
  • argument
  • method signature
  • overloading
  • call by value
  • procedural abstraction

Methods and procedural abstraction

A method is a named block of code (code inside curly braces) that runs only when it's called. Defining a method doesn't run it.

Procedural abstraction means you can use a method knowing only what it does, not how it does it. You've already done this: you call System.out.println without ever seeing its code.

Parameters, arguments and signatures

A parameter is a variable declared in the method header, and it's used inside the method. An argument is the actual value you pass in when you call it. In twice(5), the argument is 5, and inside twice the parameter n holds 5.

A method's signature is its name plus the list of its parameter types, in order. The signature of greet(String name) is greet(String); for a method with no parameters it's the name with empty parentheses, like greet(). The return type is not part of the signature.

Methods are overloaded when several have the same name but different signatures. Java picks the one whose parameter list matches the arguments. Here are three methods you'll use in the examples:

public static void greet() { System.out.println("Hi!"); } public static void greet(String name) { System.out.println("Hi, " + name + "!"); } public static int twice(int n) { return n * 2; }

greet() and greet(String) are overloaded. Calling greet("Bo") runs the second one.

void and non-void methods

A void method does a job but returns no value, so it can't be used as part of an expression. int y = greet("Ana"); won't compile.

A non-void method returns a value of the type in its header. To use that value, store it in a variable or use it in an expression: int x = twice(5); or System.out.println(twice(5) + 1);. If you just write twice(5); on its own line, the 10 is thrown away.

Arguments must match the parameter list in number, order and type. twice(4.5) won't compile, because a double can't be passed where an int is required.

Call by value and flow of control

Java passes arguments by value: each parameter starts as a copy of its argument. Changing the parameter inside the method doesn't change the caller's variable.

public static void addTen(int value) { value += 10; }

If score is 5 and you call addTen(score), score is still 5 afterward. Only the copy, value, became 15.

A method call interrupts the normal top-to-bottom order. Java jumps into the method, runs its statements until it reaches the end or a return, then comes back to the point right after the call and carries on.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Tracing calls to overloaded methods

    Using the methods greet(), greet(String name) and twice(int n) defined above, what does this code print?System.out.println("start"); greet("Ana"); int x = twice(5); greet(); System.out.println(twice(x) + 1);

    Show the solution
    1. Step 1: Line 1 prints start.
    2. Step 2: greet("Ana") has one String argument, so it matches greet(String). Control jumps into that method, which prints Hi, Ana!, then returns to the next line.
    3. Step 3: twice(5) returns 10, which is stored in x. Nothing is printed.
    4. Step 4: greet() has no arguments, so it matches the version with no parameters and prints Hi!.
    5. Step 5: twice(x) is twice(10), which returns 20. Then 20 + 1 is 21, which is printed.

    Answer: Four lines: start, Hi, Ana!, Hi! and 21.

  2. Example 2

    Call by value

    Using addTen from above, what does this code print?int score = 5; addTen(score); System.out.println(score);

    Show the solution
    1. Step 1: When addTen(score) is called, the parameter value gets a copy of 5.
    2. Step 2: value += 10; changes the copy to 15. The variable score back in the calling code is a separate variable.
    3. Step 3: When the method ends, value disappears. score was never touched.

    Answer: It prints 5. To get 15, the method would need to return the new value, and the caller would store it: score = addTen(score); with a non-void version of the method.

Common mistakes

  • Thinking the return type is part of the signature. Two methods with the same name and parameter types but different return types can't both exist.
  • Calling a non-void method and ignoring the result. twice(x); alone does nothing useful; write x = twice(x);.
  • Expecting a method to change a primitive argument. Parameters are copies.
  • Mixing up parameters (in the header) and arguments (in the call).

On the exam

  • Questions often show several overloaded methods and ask which one a call runs or what it prints. Match the number and types of the arguments to each signature.
  • In free-response questions, call the provided methods exactly as their headers say, and use their return values. Rewriting a provided method header with different parameters costs points.

Connected topics

Videos

Check yourself

4 questions on 1.9 Method Signatures. Pick an answer to see if you got it, and why.

Question 1 of 4

A class contains the following method.public static int area(int w, int h) { return w * h; }Which of the following methods, if added to the same class, causes a compile-time error?

Question 2 of 4

Consider the following method.public static void addBonus(int points) { points = points + 5; }The following code segment appears in another method of the same class.int score = 10; addBonus(score); System.out.println(score);What is printed as a result of executing the code segment?

Question 3 of 4

Consider the following method.public static double average(int a, int b, int c) { return (a + b + c) / 3.0; }Which of the following statements, appearing in another method of the same class, compiles without error?

Question 4 of 4

Consider the following method.public static int triple(int n) { return n * 3; }The following code segment appears in another method of the same class.int a = triple(2); int b = triple(a) - a; System.out.println(a + " " + b);What is printed as a result of executing the code segment?

0 of 4 answered