1.9 - Method Signatures
A method is a named block of code that only runs when it’s called. A block of code is any chunk of code wrapped in braces
{ }. Procedural abstraction means you can use a method just by knowing what it does, you don’t need to know how it’s actually implemented under the hood.You’ve done this already. When you call
Math.sqrt(25.0), you get 5.0 back without knowing the steps Java uses to find a square root. All you need is the method’s name, what it takes in (its parameters), and what it gives back.A parameter is a variable declared in a method or constructor’s header, and you can use it inside the method’s body. It lets you pass values in when the method gets called.
A method signature consists of the method’s name plus the ordered list of parameter types. If a method takes no parameters, the signature is just the name with an empty parameter list.
// method signature: calculateArea(double, double) public double calculateArea(double width, double height) { return width * height; }
Here’s how the pieces of that method header line up:
public: who’s allowed to call it (more in Unit 3).
double: the return type, the type of value the method gives back (voidif it gives nothing back).
calculateArea: the method name.
(double width, double height): the parameter list, the type and name of each parameter.
Only the name and the parameter types make up the signature. The return type and the parameter names are not part of it, so a header like
public int calculateArea(double w, double h) has the same signature, calculateArea(double, double).A void method has no return value, so it can’t be used as part of an expression, you just call it on its own.
A non-void method returns a value matching its declared return type. To actually use that returned value, you need to store it in a variable or use it inside an expression.
void printGreeting() { // void, just does something System.out.println("Hi!"); } int square(int x) { // non-void, returns a value return x * x; } int result = square(4); // storing the returned value
square(4); // legal, but the returned 16 is thrown away int y = square(4) + 1; // 17, the return value is used in an expression int z = printGreeting(); // won't compile, a void method has no value to store
An argument is the actual value you pass into a method when you call it. The arguments have to match the parameter list in number and order. Java uses call by value, meaning the parameters get initialized with copies of whatever arguments you passed in, not the originals themselves.
Compatible means each argument has to work with its parameter’s type, in order. An
int argument can go into a double parameter (it gets widened), but not the other way around:calculateArea(3.0, 4.5); // returns 13.5 calculateArea(3, 4); // returns 12.0, the ints 3 and 4 are widened to doubles calculateArea(3.0); // won't compile, too few arguments calculateArea("3", "4"); // won't compile, Strings aren't doubles
Call by value means the method gets its own copy of each argument. Changing the parameter inside the method doesn’t change the variable that was passed in:
public static void addTen(int num) { num = num + 10; System.out.println("inside: " + num); } // somewhere else int score = 5; addTen(score); // prints inside: 15 System.out.println("outside: " + score); // prints outside: 5
Methods are overloaded when there are multiple versions with the same name but different signatures (different parameter types or counts).
// Overloaded methods, same name, different signatures int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; }
Java picks which version to run by matching the arguments to the signatures:
add(2, 3); // calls add(int, int), returns 5 add(2.5, 1.5); // calls add(double, double), returns 4.0 add(2, 3.5); // calls add(double, double), the 2 is widened, returns 5.5
Two methods can’t differ only in their return type.
int add(int a, int b) and double add(int a, int b) have the same signature, so they can’t both be in the same class.When a method gets called, it interrupts whatever was running sequentially, runs its own statements, and then control returns right back to where it left off once the method finishes or hits a
return statement.public class FlowDemo { public static void middle() { System.out.println("2"); System.out.println("3"); } public static void main(String[] args) { System.out.println("1"); middle(); System.out.println("4"); } }
Output:
1 2 3 4
When
main reaches the call to middle, it pauses, runs both lines inside middle, and then picks up right after the call.