120 likes | 133 Views
05 Method Calling. Outline. What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value. Objectives. Define a method Demonstrate how to properly declare a method. Demonstrate how methods call each other
E N D
Outline • What is a Method? • Declaring a Method • Method Calling • Method Call Stack • Parameter Passing • Pass by Value
Objectives • Define a method • Demonstrate how to properly declare a method. • Demonstrate how methods call each other • Demonstrate how methods are executed in call stack • Demonstrate parameter passing • Understand that Java passes arguments by value.
What is a Method? • A method refers to a piece of code referring to behaviors associated either with an object or its class • A code found in a class for responding to a message • The executable code that implements the logic of a particular message for a class • An operation or function that is associated with an object and is allowed to manipulate the object's data
Steps in declaring a method Set the return type Provide method name Declare formal parameters class Number { int multiply(int i, int j) { return i*j; } int divide(int i, int j) { return i/j; } double getPi() { return 3.14159265358979; } void printSum(int i, int j) { System.out.println(i+j); } } Creating a Method method signature • consists of the method name and its parameters • must be unique for each method in a class return statement • allows the method to return a value to its caller • also means to stop the execution of the current method and return to its caller • implicit return at the end of the method A method with empty parameters A method that does not return a value must specify void as its return type
Method Calling • How to call a method • Method name should match • Number of parameters should match • Type of parameters should match • Ways of calling a method • Calling a method through its object name • Calling a method within the same class • Calling a static method through its class name
public class JavaMain { public static void main(String[] args) { // create a Person object Person you = new Person(); you.talk(); you.jump(3); System.out.println(you.tellAge()); JavaMain.talkOnly(you); // create object of main program JavaMain me = new JavaMain(); me.jumpOnly(you); } static void talkOnly(Person p) { p.talk(); } voidjumpOnly(Person p) { p.jump(2); } } class Person { void talk() { System.out.println("blah, blah..."); } void jump(int times) { for (int i=0; i<times; i++) { System.out.println("whoop!"); } } String tellAge() { return"I'm " + getAge(); } int getAge() { return 10; } } Method Calling - Example blah, blah... whoop! whoop! whoop! I'm 10 blah, blah... whoop! whoop!
Method Call Stack • The Method Call Stack refers to all the methods currently active and being processed by a Java application invoke print() invoke compute() invoke check() main() begins execute print() execute compute() execute check() main() ends
Passing Parameters Passing parameters in Java is alwaysPass by Value! When passing a parameter of primitive type: • A copy of the value of the variable is passed • The passed variable cannot be changed in the called method as the method only possesses a copy of that variable. When passing a parameter of reference type: • A copy of the reference (address) of the object is passed • The object reference cannot be changed in the called method (i.e., the object cannot be reassigned to another object) • The object state can be changed in the called method (i.e., attributes can be modified)
Passing Parameters – Strings • String literals in Java are implemented as instances of String class (java.lang.String). • Strings in java are immutable (i.e., their value cannot be changed). • When passing a parameter of String type: • A new instance of String class is created and the value of the parameter is copied to the new instance. • Like parameters of primitive type, the passed variable cannot be changed in the called method because the method only possesses a copy of that variable.
public class TestJavaParameterPassing { public static void main(String[] args) { int count=5; int[] numbers = {10,12,15}; String name = “Tom"; TestJavaParameterPassing test = new TestJavaParameterPassing(); System.out.println(“Passing a parameter of primitive type (count)."); System.out.println(“ Initial Value of count in main is: “ count); test.changeCount (count); System.out.println(“ Value of count in main remains unchanged after calling changeCount: " + count); System.out.println(“Passing a parameter of reference type (numbers)."); System.out.println(“ Initial Value of numbers in main is: “ + test.get.Numbers(numbers)); test.changeNumbers (numbers); System.out.println(“ Values of numbers in main changes after calling changeNumbers: " + test.getNumbers (numbers)); System.out.println(“Passing a parameter of String type (name)."); System.out.println(“ Initial Value of name in main is: “ + name); test.changeName(name); System.out.println(“ Values of name in main remains unchanged after calling changeName: " + name); } void changeCount(int count) { count += 5; System.out.println(“ Value of count parameter in changeCount method after the change is: " + count); } void changeNumbers(int[] numbers) { numbers [0]=20; numbers [1]=11; numbers [2]=18; System.out.println(“ Value of numbers in changeNumbers method after the change is: " + getNumbers(numbers)); } String getNumbers(int[] numbers) { String s=""; for (int i=0; i<numbers.length; i++) s += numbers[i] + " "; return s; } void changeName(String name) { String newName = “Tony"; name = newName; System.out.println(“ Value of name in changeName method after the change is: " + name); } } Passing Parameters - Example Passing a parameter of primitive type (count). Initial Value of count in main is: 5 Value of count parameter in changeCount method after the change is: 10 Value of count in main remains unchanged after calling changeCount: 5 Passing a parameter of reference type (numbers). Initial Value of numbers in main is: 10 12 15 Value of numbers in changeNumbers method after the change is: 20 11 18 Values of numbers in main changes after calling changeNumbers: 20 11 18 Passing a parameter of String type (name). Initial Value of name in main is: Tom Value of name in changeName method after the change is: Tony Values of name in main remains unchanged after calling changeName: Tom
Key Points • A method refers to what an object or class can do • A method must have a return type, a name, and optional parameters • The method signature refers to a method name and its parameters • Return statement returns a value to its caller or returns control to its caller • A method that does not return a value must specify void as a return type • Calling a method should match its method signature • When calling a method in the same class, use only the method name • When calling a method in a different class, use the object reference • When calling a static method, use the class name • Methods are invoked sequentially in the call stack and executed in reverse order • Passing parameters in Java is always pass by value