"Take a method and try it. If it fails, admit it frankly, and try another. But by all means, try something."
Methods are modular reusable blocks of code.
Methods are modular reusable blocks of code.
Methods are modular reusable blocks of code.
The control flow of a program can easily switch to code within a method from anywhere else in the code.
Switching to code within a method is termed 'calling' or 'invoking' that method.
Methods are modular reusable blocks of code.
The control flow of a program can easily switch to code within a method from anywhere else in the code.
Switching to code within a method is termed 'calling' or 'invoking' that method.
Calling a method is a form of unconditional branching - disrupting the 'usual' flow of a program.
Methods are modular reusable blocks of code.
The control flow of a program can easily switch to code within a method from anywhere else in the code.
Switching to code within a method is termed 'calling' or 'invoking' that method.
Calling a method is a form of unconditional branching - disrupting the 'usual' flow of a program.
Once the control flow reaches the end of an invoked method, the control flow returns to the line of code from which it originally branched.
methods allow us to split a large program into manageable (read: small) sub-problems - "divide and conquer".
modular code is easier to read.
methods allow us to split a large program into manageable (read: small) sub-problems - "divide and conquer".
modular code is easier to read.
modular code is re-usable code.
methods allow us to split a large program into manageable (read: small) sub-problems - "divide and conquer".
modular code is easier to read.
modular code is re-usable code.
modular code is easier to write and debug.
A user (e.g. you) can use a method without knowing its implementation. The only thing the user needs to know is the methods signature (how to call it) and return type.
If the implementation changes "behind the scenes", the user does not need to modify their code that builds on the method (as long as the signature did not change).
At their simplest, methods can have no parameters and no return value.
For example, a method named 'doSomething1':
public static void doSomething1() { System.out.println("Running doSomething1"); // imagine some useful stuff happens in the middle here System.out.println("Exiting doSomething1");}
Methods are comprised of a header and a body. Syntax:
modifiers returnType methodName(parameters) { [...] // stuff happens here return result;}
The returnType
can be any data type or void
(i.e. the method does not return a value).
Of course, methods can call other methods, as our doSomething1
already displayed - it called the System.out.println
method several times.
Note the order in which the print statements are executed.
public static void doSomething1() { System.out.println("Begin doSomething1"); // imagine some useful stuff happens in the middle here System.out.println("End doSomething1");}public static void doSomething2() { System.out.println("Begin doSommething2"); doSomething1(); // call the method System.out.println("End doSomething2");}
The order is determined by the control flow of the program.
The order is determined by the control flow of the program.
The order is determined by the control flow of the program.
Each method invocation creates a new 'stack frame' - an area of memory dedicated to the newly invoked method.
The Java interpreter is only ever looking at the method invocation at the top of the stack - the most recently invoked method.
The order is determined by the control flow of the program.
Each method invocation creates a new 'stack frame' - an area of memory dedicated to the newly invoked method.
The Java interpreter is only ever looking at the method invocation at the top of the stack - the most recently invoked method.
Each stack frame has its own variable namespace, so you can have two variables named x
in two different method invocations, and they will be different variables in two different areas of memory with potentially different values.
The order is determined by the control flow of the program.
Each method invocation creates a new 'stack frame' - an area of memory dedicated to the newly invoked method.
The Java interpreter is only ever looking at the method invocation at the top of the stack - the most recently invoked method.
Each stack frame has its own variable namespace, so you can have two variables named x
in two different method invocations, and they will be different variables in two different areas of memory with potentially different values.
Once a method that has been invoked completes, its stack frame is popped (deleted) from the call stack and its memory is wiped clean.
Methods can accept 'arguments' - values sent into the method.
Methods can accept 'arguments' - values sent into the method.
Methods can accept 'arguments' - values sent into the method.
These arguments are stored in 'parameters' - local variables within the namespace of the method invocation's stack frame.
Once the method invocation completes, any local variables, including parameters, are wiped out of memory.
Methods can accept 'arguments' - values sent into the method.
These arguments are stored in 'parameters' - local variables within the namespace of the method invocation's stack frame.
Once the method invocation completes, any local variables, including parameters, are wiped out of memory.
public static void doSomething1(int x) { x++; // increment x System.out.println("Begin doSomething1, x=" + x); System.out.println("End doSomething1, x=" + x);}
Arguments are passed by value. Their value gets copied to variables that are specific to that method.
Arguments are passed by value. Their value gets copied to variables that are specific to that method.
public static void addOne(int x) { x++; // increment x}public static void main(String[] args) { int i = 1; addOne(i); System.out.println("i is " + i); // i is 1}
This naming can be confusing, since the value of an array variable is (essentially) its address in memory:
public static void addOne(int[] array) { for (int i = 0; i < array.length; i++) { array[i]++; } } public static void printarray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i]); } System.out.print("\n"); } public static void main(String[] args) { int[] array = {1,2,3}; printarray(array); // prints 123 addOne(array); printarray(array); // prints 234 }
We will discuss this further in the coming weeks.
Methods can return a single value in Java.
Methods can return a single value in Java.
Methods can return a single value in Java.
The method signature line must state the data type of the returned value
This value is returned to the part of the code from which the method was originally invoked.
Methods can return a single value in Java.
The method signature line must state the data type of the returned value
This value is returned to the part of the code from which the method was originally invoked.
Think of it as if the method invocation is replaced by that method's return value.
Methods can return a single value in Java.
The method signature line must state the data type of the returned value
This value is returned to the part of the code from which the method was originally invoked.
Think of it as if the method invocation is replaced by that method's return value.
public static int doSomething1(int x) { x++; // increment x System.out.println("Begin doSomething1, x=" + x); System.out.println("End doSomething1, x=" + x); return x;}
Java allows multiple methods with the same name but different parameter sets. These are called overloaded methods.
Java allows multiple methods with the same name but different parameter sets. These are called overloaded methods.
Java allows multiple methods with the same name but different parameter sets. These are called overloaded methods.
public static void foo() { ...}public static void foo(String bar) { ...}public static void foo(String bar, boolean baz) { ...}
Javadoc allows us to write documentation so users understand what our methods do. Take the following example:
/** * Computing the square root with Heron's method. * @param x a strictly positive number. * @return the square root of x. */public double myRoot(double x) { if (x == 0.0) { return x; } else if (x < 0.0) { System.out.println("Error: Negative number entered."); return 0.0; // this is not ideal but we will discuss exception handling later } int max_it = 10; double s = 0.5 * x; for (int i = 1; i <= max_it; i++) { s = 0.5 * (s + x/s); } return s;}
You now have a basic understanding of methods in Java.
You now have a basic understanding of methods in Java.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |