Presentation is loading. Please wait.

Presentation is loading. Please wait.

05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.

Similar presentations


Presentation on theme: "05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline."— Presentation transcript:

1 05 Method Calling

2 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline

3 3 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.

4 4 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

5 5 Creating a Method Steps in declaring a method 1.Set the return type 2.Provide method name 3.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); } } 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 method signature consists of the method name and its parameters must be unique for each method in a class A method with empty parameters A method that does not return a value must specify void as its return type

6 6 Method Calling How to call a method 1.Method name should match 2.Number of parameters should match 3.Type of parameters should match Ways of calling a method 1.Calling a method through its object name 2.Calling a method within the same class 3.Calling a static method through its class name

7 7 Method Calling - Example 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(); } void jumpOnly(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; } } blah, blah... whoop! I'm 10 blah, blah... whoop!

8 8 Method Call Stack print() compute() check() main() invoke print() invoke compute() invoke check() main() begins execute print() execute compute() execute check() main() ends The Method Call Stack refers to all the methods currently active and being processed by a Java application

9 9 Passing Parameters Passing parameters in Java is always Pass 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)

10 10 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.

11 11 Passing Parameters - Example 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 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

12 12 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


Download ppt "05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline."

Similar presentations


Ads by Google