Methods Notes PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides a summary of methods in programming. It describes how methods are used to perform operations and differentiates between void methods and non-void methods.
Full Transcript
Methods Method: a collection of statements grouped together to perform an operation Examples: Math.random() str.length() System.out.println() str.indexOf() JOptionPane.showInputDialog() System.exit() Also, you can write your own methods! Your methods may be on...
Methods Method: a collection of statements grouped together to perform an operation Examples: Math.random() str.length() System.out.println() str.indexOf() JOptionPane.showInputDialog() System.exit() Also, you can write your own methods! Your methods may be one of two types: Void – does not return any value o Often print results from void methods to display output Non-void – returns a value of the same type specified in the method header Non-void Examples: Void Examples: public class SayHi1{ public c public static void main(String[] args){ public String hi = howdy(); howdy( System.out.println(hi); } } public public static String howdy(){ String public class String str TestMax1{ = “Hello!”; public c System public return str; void main(String[] args){ static public } }int i = 5; } int i int j } int j = 2; int k = max(i, j); max(i, System.out.println("Max of " + i + } " and " + j + " is " + k); } public s public static int max(int num1, int num2){ int re Basic differences? Non-void returns a result to main, where it is then displayed; void computes and displays a result within the method itself without returning anything to main The return value of non-void methods must be stored in a variable or directly printed; void method calls can stand on their own Calling: To use a method, your code must ‘call’ it To call a method, use its name and pass values for the parameters as required Any time a method is invoked, the system stores parameters and variables in an area of memory known as the stack o The stack stores elements in a ‘last-in, first-out’ (LIFO) fashion o When your main method calls another method, the caller’s stack is kept intact, while new space is created to handle the new method call o When the secondary method finishes its work, it returns program control to the original calling method, and associated memory (stack) space is released Passing Parameters by Values When you call a method, you’re actually providing arguments Arguments are values that will be used to fill the parameters referenced in your method header o Programmers often get lazy and call both the items in () within the method header AND the items passed in the method call parameters – but the latter are technically arguments Arguments must be passed in the same order and of the same type as the parameters listed in the method header When you call/invoke a method whose header contains parameters, the value of the argument is passed to the parameter o This is referred to as pass-by-value, and simply means you are making a copy of the parameter’s actual value in memory o This is opposed to pass-by-reference, which means you are making a copy of the address of the parameter’s value Overloading Methods You know how we can’t name two classes within one package the same name? You know how we can’t name two variables within one class the same name? Well, the same is true for methods… o What if we created a method for finding the max of two integer values, but then we realized that sometimes, we need to find the max of two double values? Or what if we have a beautiful method for finding the average of two numbers, but then we realize we sometimes need to find the value of three numbers? o We can define multiple methods of the same name within the same class as long as the argument lists meet one of three differences: ▪ Number of params ▪ Type of params ▪ Sequence of type of params o This is known as overloading o See next page for example Overloading example: public class TestMax1{ public static void main(String[] args){ int i = 5, j = 2; double x = 5.5, y = 2.2; max(i, j); max(x, y); } public static void max(int num1, int num2){ if(num1 > num2){ System.out.println(num1); } else{ System.out.println(num2); } } public static void max(double num1, double num2){ if(num1 > num2){ System.out.println(num1); In summary: overloading = when we have multiple methods} that have the same name, but accept different params else{ Variable Scope System.out.println(num2); } Scope of a variable is the part of the program where the variable can be referenced } A variable defined inside a method is known as a local variable } o Scope of a local variable starts from where it is declared and continues to the end of the block that contains the method o In the original max examples, int i is a local variable (local to main method) and int result is a local variable (local to the max method) Variables declared outside of any methods (typically at the top of your program, directly beneath your class declaration) are known as global variables, and they can be used throughout the entire program – every method can see them Static vs. Non-static Methods A static method belongs to a class – in other words, we don’t need any pre-created instance of an object in order to invoke the method o Think about the main method – public static void main…you don’t have to do anything special for it to run, and it runs the same way every time A non-static method belongs to an object (or an instance) of a class – in other words, an object of the class that the non-static method belongs to must first be created before the non-static method can be invoked upon said object Which of the following methods are static? o length() o substring() o pow() o sqrt() Answer: pow() and sqrt(). The first two require specific instances of String objects be created in order for them to work. The last two work without any object first being created, you can simply store them back into a primitive variable or view the results via a print statement.