Presentation is loading. Please wait.

Presentation is loading. Please wait.

FIT1002 2006 1 Objectives By the end of this lecture, students should: understand how parameters are passed to and from methods understand what the differences.

Similar presentations


Presentation on theme: "FIT1002 2006 1 Objectives By the end of this lecture, students should: understand how parameters are passed to and from methods understand what the differences."— Presentation transcript:

1 FIT1002 2006 1 Objectives By the end of this lecture, students should: understand how parameters are passed to and from methods understand what the differences between reference types and basic types are in parameter passing understand the concept of “privacy leaks” be able to analyze how references are passed in a Java program and use the different passing mechanisms as required. Reading: Savitch, Sec. 5.2, 5.3 (if necessary refresh Sec. 4.1)

2 FIT1002 2006 2 Method Calls a method call is essentially evaluated by executing the method and replacing the return value into the calling expression. If the return value is not used, it will just be ignored (ie. the method is effectively used like a void method). Person x = new Person(); System.out.println(x.getAge() + 10);

3 FIT1002 2006 3 Method Calls a method call is essentially evaluated by executing the method and replacing the return value into the calling expression. If the return value is not used, it will just be ignored (ie. the method is effectively used like a void method). Person x = new Person(); System.out.println(20 + 10);

4 FIT1002 2006 4 Method Calls a method call is essentially evaluated by executing the method and replacing the return value into the calling expression. If the return value is not used, it will just be ignored (ie. the method is effectively used like a void method). Person x = new Person(); System.out.println(30);

5 FIT1002 2006 5 Method Calls in Complex Expressions a method call can return another object on which, in turn, a method can be called: Person x = new Person(); Person y = new Person(); x.partner = y; System.out.println(x.getPartner().getAge()+10);

6 FIT1002 2006 6 Method Calls in Complex Expressions a method call can return another object on which, in turn, a method can be called: Person x = new Person(); Person y = new Person(); x.partner = y; System.out.println(.getAge()+10);

7 FIT1002 2006 7 Method Calls in Complex Expressions a method call can return another object on which, in turn, a method can be called: Person x = new Person(); Person y = new Person(); x.partner = y; System.out.println(20+10);

8 FIT1002 2006 8 Method Calls in Complex Expressions a method call can return another object on which, in turn, a method can be called: Person x = new Person(); Person y = new Person(); x.partner = y; System.out.println(30);

9 FIT1002 2006 9 Call Parameters of Basic Types When a method with parameters is called, call proceeds as follows: the actual parameter expressions are evaluated the method is called and creates local variables for its formal parameters. these local variables are bound to the values of the actual parameters double d = test(3+4, 5/2.0); public double test(int x, double y) { double result = x / y; return result; }

10 FIT1002 2006 10 Call Parameters of Basic Types When a method with parameters is called, call proceeds as follows: the actual parameter expressions are evaluated the method is called and creates local variables for its formal parameters. these local variables are bound to the values of the actual parameters double d = test(7, 5/2.0); public double test(int x, double y) { double result = x / y; return result; }

11 FIT1002 2006 11 Call Parameters of Basic Types When a method with parameters is called, call proceeds as follows: the actual parameter expressions are evaluated the method is called and creates local variables for its formal parameters. these local variables are bound to the values of the actual parameters double d = test(7, 2.5); public double test(int x, double y) { double result = x / y; return result; }

12 FIT1002 2006 12 Call Parameters of Basic Types When a method with parameters is called, call proceeds as follows: the actual parameter expressions are evaluated the method is called and creates local variables for its formal parameters. these local variables are bound to the values of the actual parameters double d = test(7, 2.5); public double test(int x, double y) { double result = x / y; return result; }

13 FIT1002 2006 13 Call Parameters of Basic Types When a method with parameters is called, call proceeds as follows: the actual parameter expressions are evaluated the method is called and creates local variables for its formal parameters. these local variables are bound to the values of the actual parameters double d = test(7, 2.5); public double test(int x, double y) { double result = x / y; return result; }

14 FIT1002 2006 14 Call Parameters of Basic Types When a method with parameters is called, call proceeds as follows: the actual parameter expressions are evaluated the method is called and creates local variables for its formal parameters. these local variables are bound to the values of the actual parameters double d = test(7, 2.5); public double test(int x, double y) { double result = 7 / 2.5; return result; }

15 FIT1002 2006 15 Call Parameters of Basic Types When a method with parameters is called, call proceeds as follows: the actual parameter expressions are evaluated the method is called and creates local variables for its formal parameters. these local variables are bound to the values of the actual parameters double d = test(7, 2.5); public double test(int x, double y) { double result = 7 / 2.5; return 2.8; }

16 FIT1002 2006 16 Call Parameters of Basic Types When a method with parameters is called, call proceeds as follows: the actual parameter expressions are evaluated the method is called and creates local variables for its formal parameters. these local variables are bound to the values of the actual parameters double d = 2.8 ; public double test(int x, double y) { double result = 7 / 2.5; return 2.8; }

17 FIT1002 2006 17 Return Values of Basic Types return values of basic types (int float, etc) are handled in the same way as call parameters with basic type. The actual value is returned and substituted in the calling expression. You can understand this in the same way as if a literal textual substitution happens.

18 FIT1002 2006 18 Call-by-Value The calling mechanism outlined above is termed “Call by Value” because a concrete value is passed to the method.

19 FIT1002 2006 19 Call Parameters of Class Types What happens if we pass an object to a method? We are effectively passing a reference to the object used in the call. We are not generating a new copy of this object! This is why a class type is called a reference type We will later meet other reference types.

20 FIT1002 2006 20 Passing a Reference Types Person p = new Person() ; System.out.println(personsAge(p)); public int personsAge(Person p1) { int result = p1.age; return result; }

21 FIT1002 2006 21 Passing a Reference Types name age partner 20 Person p = new Person() ; System.out.println(personsAge(p)); public int personsAge(Person p1) { int result = p1.age; return result; }

22 FIT1002 2006 22 Passing a Reference Types name age partner 20 p Person p = new Person() ; System.out.println(personsAge(p)); public int personsAge(Person p1) { int result = p1.age; return result; }

23 FIT1002 2006 23 Passing a Reference Types Person p = new Person() ; System.out.println(personsAge(p)); public int personsAge(Person p1) { int result = p1.age; return result; } name age partner 20 p

24 FIT1002 2006 24 Passing a Reference Types Person p = new Person() ; System.out.println(personsAge(p)); public int personsAge(Person p1) { int result = p1.age; return result; } name age partner 20 p

25 FIT1002 2006 25 Passing a Reference Types Person p = new Person() ; System.out.println(personsAge(p)); public int personsAge(Person p1) { int result = p1.age; return result; } name age partner 20 p

26 FIT1002 2006 26 Passing a Reference Types Person p = new Person() ; System.out.println(personsAge(p)); public int personsAge(Person p1) { int result = 20; return result; } name age partner 20 p

27 FIT1002 2006 27 Passing a Reference Types Person p = new Person() ; System.out.println(personsAge(p)); public int personsAge(Person p1) { int result = 20; return 20; } name age partner 20 p

28 FIT1002 2006 28 Passing a Reference Types Person p = new Person() ; System.out.println(20); public int personsAge(Person p1) { int result = 20; return 20; } name age partner 20 p

29 FIT1002 2006 29 Call-by-Reference The calling mechanism outlined for object parameters is termed “Call by Reference” because a reference to the actual object is passed to the method. No copy of the object is generated!

30 FIT1002 2006 30 Consequences of Call by Reference The called method has a reference to the original object and can thus fully access (and change) it in as far as the fields/methods are visible. Assuming that age is a public instance variable. What happens here? Person p = new Person() ; System.out.println(p.age); mystery(p); System.out.println(p.age); public void mystery(Person p1) { p1.age = 99; }

31 FIT1002 2006 31 Consequences of Call by Reference Person p = new Person() ; System.out.println(p.age); mystery(p); System.out.println(p.age); public void mystery(Person p1) { p1.age = 99; } name age partner 0 p

32 FIT1002 2006 32 Consequences of Call by Reference Person p = new Person() ; System.out.println(p.age); mystery(p); System.out.println(p.age); public void mystery(Person p1) { p1.age = 99; } name age partner 99 p

33 FIT1002 2006 33 Consequences of Call by Reference Person p = new Person() ; System.out.println(p.age); mystery(p); System.out.println(p.age); public void mystery(Person p1) { p1.age = 99; } name age partner 99 p

34 FIT1002 2006 34 Returning a Reference Type The same mechanism is used when an object is returned. Consider a get method for the partner field of the class Person: public class Person { private String name; private int age; private Person partner; … public Person getPartner() { return partner; }

35 FIT1002 2006 35 Returning a Reference Type What does the following code do? Person somebody = new Person(); somebody.setPartner(new Person()); Person aPartner = somebody.getPartner(); aPartner.setAge(101); System.out.println(somebody.getPartner.getAge());

36 FIT1002 2006 36 Returning a Reference Type You have changed the age of the original object. Person somebody = new Person(); somebody.setPartner(new Person()); Person aPartner = somebody.getPartner(); aPartner.setAge(101); System.out.println(somebody.getPartner.getAge()); name age partner 20 aPartner name age partner 101 somebody

37 FIT1002 2006 37 Privacy Leaks You have just used a getMethod for a private instance variable to change the value of this variable. This should really not be permitted, such a situation is called a “privacy leak”. The only way to make sure that this cannot happen is to create a copy of the object and return this copy. In this way there is no access to the original object.

38 FIT1002 2006 38 Avoiding Privacy Leaks Create a copy. You can do this easily by creating a shadow copy in the get method. public class Person { private String name; private int age; private Person partner; public getPartner() { Person shadow = new Person(); shadow.name=partner.name; shadow.age=partner.age; shadow.partner=partner.partner; return shadow; } … }

39 FIT1002 2006 39 Consequences for get Methods If you really mean “private” to mean private: any getMethod for an object- valued attribute must return a copy of the object. This is the only safe way to protect the object from being changed from the outside. However, this also costs time and memory because a new object is allocated.

40 FIT1002 2006 40 Copy Constructors (Advanced, optional) Generating a copied object can be done more elegantly by defining a copy constructor (as below). We will return to this when we discuss constructors. public class Person { private String name; private int age; private Person partner; public Person(Person original) { this.name=original.Name; this.age=original.age; this.partner=original.partner; } … public Person getPartner() { return new Person(partner); }

41 FIT1002 2006 41 Reading Savitch Sec. 5.2, 5.3 (Reference Passing)


Download ppt "FIT1002 2006 1 Objectives By the end of this lecture, students should: understand how parameters are passed to and from methods understand what the differences."

Similar presentations


Ads by Google