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.

Slides:



Advertisements
Similar presentations
This is Java Jeopardy Writing Methods…chapter 4…
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
PLDI’2005Page 1June 2005 Example (C code) int double(int x) { return 2 * x; } void test_me(int x, int y) { int z = double(x); if (z==y) { if (y == x+10)
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Memory Management & Method Calls in Java Program Execution © Allan C. Milne v
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Enhancing classes Visibility modifiers and encapsulation revisited
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Initialization George Blank Subba Reddy Daka. Importance of Initialization Java classes are initialized and have predictable default values. These values.
Supplementary for method, DCO10803, Quarter 3, Page 1 of 7 Object-Oriented Programming and Design DCO10803 Supplementary for method  Prototype.
Understanding class definitions Looking inside classes.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Values, variables and types © Allan C. Milne v
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Session 7 Methods Strings Constructors this Inheritance.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Building java programs, chapter 3 Parameters, Methods and Objects.
Classes, Interfaces and Packages
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
AKA the birth, life, and death of variables.
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Java Programming: Guided Learning with Early Objects
The dirty secrets of objects
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Variables and Their scope
Classes, Encapsulation, Methods and Constructors (Continued)
Array and Method.
Chapter 4 Topics: class declarations method declarations
AKA the birth, life, and death of variables.
Building Java Programs
Method of Classes Chapter 7, page 155 Lecture /4/6.
Java Programming Language
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
References Revisted (Ch 5)
Visibilities and Static-ness
Corresponds with Chapter 5
Presentation transcript:

FIT 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)

FIT 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);

FIT 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( );

FIT 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);

FIT 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);

FIT 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);

FIT 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);

FIT 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);

FIT 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; }

FIT 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; }

FIT 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; }

FIT 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; }

FIT 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; }

FIT 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; }

FIT 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; }

FIT 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; }

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

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

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

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

FIT 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; }

FIT 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; }

FIT 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

FIT 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

FIT 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

FIT 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

FIT 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

FIT 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

FIT 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!

FIT 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; }

FIT 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

FIT 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

FIT 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

FIT 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; }

FIT 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());

FIT 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

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

FIT 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; } … }

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

FIT 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); }

FIT Reading Savitch Sec. 5.2, 5.3 (Reference Passing)