Understanding Parameter Passing. Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a.

Slides:



Advertisements
Similar presentations
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Advertisements

1 Procedural Programming Paradigm Stacks and Procedures.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Parameter Passing Jason Fullowan, Mariusz G. Kedziora, George Blank.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Procedures and Control Flow CS351 – Programming Paradigms.
Passing information through Parameters ( our setter methods or mutators) Formal Parameter – parameter in the method. public void SetA(int x){ (int x) is.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Road Map Introduction to object oriented programming. Classes
Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Procedural programming in Java Methods, parameters and return values.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Building java programs, chapter 3 Parameters, Methods and Objects.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Week 12 Methods for passing actual parameters to formal parameters.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Computer Organization and Design Pointers, Arrays and Strings in C
User-Written Functions
Chapter 7 User-Defined Methods.
Intro To Classes Review
Chapter 8 Classes and Objects
Advanced Programming Behnam Hatami Fall 2017.
March 29th Odds & Ends CS 239.
An Introduction to Java – Part II
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Group Status Project Status.
Defining methods and more arrays
Subroutines – parameter passing
Parameter Passing in Java
Understanding Parameter Passing
Chapter 6 Methods.
Java Classes Aliases & Null & This
Simulating Reference Parameters in C
Why did the programmer quit his job?
Lecture 11 Parameters CSE /26/2018.
A+ Computer Science PARAMETERS
Introduction to Computer Science and Object-Oriented Programming
C Parameter Passing.
Presentation transcript:

Understanding Parameter Passing

Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a parameter passed to a method, a separate copy of the value is passed. Any changes made to the parameter passed into the method will have no effect on the actual parameter.

Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a parameter passed to a method, a separate copy of the value is passed. Any changes made to the parameter passed into the method will have no effect on the actual parameter. zJava passes all parameters to a method by value.

Parameters are Passed by Value zIt is important to understand how parameter passing works. When you make changes to a parameter passed to a method, a separate copy of the value is passed. Any changes made to the parameter passed into the method will have no effect on the actual parameter. zJava passes all parameters to a method by value. zThis means that the current value of the actual parameter is copied into the formal parameter in the method header.

An Example Using Primitive Data Types public class PrimitiveParamTest { public void changeValue (int parameter) { parameter = 100; System.out.println("Inside method changeValue, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; PrimitiveParamTest test = new PrimitiveParamTest(); System.out.println("Initial parameter value is " + parameter); test.changeValue(parameter); System.out.println("After method changeValue call, parameter value is " + parameter); } Using the code at left, we will demonstrate how primitive data types are handled as parameters in method calls.

An Example Using Primitive Data Types public class PrimitiveParamTest { public void changeValue (int parameter) { parameter = 100; System.out.println("Inside method changeValue, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; PrimitiveParamTest test = new PrimitiveParamTest(); System.out.println("Initial parameter value is " + parameter); test.changeValue(parameter); System.out.println("After method changeValue call, parameter value is " + parameter); } The program begins by assigning the value 25 to the integer variable parameter. 25 parameter value Output :

An Example Using Primitive Data Types public class PrimitiveParamTest { public void changeValue (int parameter) { parameter = 100; System.out.println("Inside method changeValue, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; PrimitiveParamTest test = new PrimitiveParamTest(); System.out.println("Initial parameter value is " + parameter); test.changeValue(parameter); System.out.println("After method changeValue call, parameter value is " + parameter); } The test object is created. 25 parameter value Output :

An Example Using Primitive Data Types public class PrimitiveParamTest { public void changeValue (int parameter) { parameter = 100; System.out.println("Inside method changeValue, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; PrimitiveParamTest test = new PrimitiveParamTest(); System.out.println("Initial parameter value is " + parameter); test.changeValue(parameter); System.out.println("After method changeValue call, parameter value is " + parameter); } The value of parameter is displayed. 25 parameter value Output : Initial parameter value is 25

An Example Using Primitive Data Types public class PrimitiveParamTest { public void changeValue (int parameter) { parameter = 100; System.out.println("Inside method changeValue, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; PrimitiveParamTest test = new PrimitiveParamTest(); System.out.println("Initial parameter value is " + parameter); test.changeValue(parameter); System.out.println("After method changeValue call, parameter value is " + parameter); } The PrimitiveParamTest object method changeValue is called. 25 parameter value Output : Initial parameter value is 25

An Example Using Primitive Data Types public class PrimitiveParamTest { public void changeValue (int parameter) { parameter = 100; System.out.println("Inside method changeValue, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; PrimitiveParamTest test = new PrimitiveParamTest(); System.out.println("Initial parameter value is " + parameter); test.changeValue(parameter); System.out.println("After method changeValue call, parameter value is " + parameter); } As a result of the method call, a copy of parameter is made. This copy is called the formal parameter. The formal parameter is sent to the method. 25 parameter value Output : Initial parameter value is formal parameter

An Example Using Primitive Data Types public class PrimitiveParamTest { public void changeValue (int parameter) { parameter = 100; System.out.println("Inside method changeValue, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; PrimitiveParamTest test = new PrimitiveParamTest(); System.out.println("Initial parameter value is " + parameter); test.changeValue(parameter); System.out.println("After method changeValue call, parameter value is " + parameter); } When the method refers to parameter, it uses the formal parameter. The formal parameter is changed to parameter value Output : Initial parameter value is formal parameter

An Example Using Primitive Data Types public class PrimitiveParamTest { public void changeValue (int parameter) { parameter = 100; System.out.println("Inside method changeValue, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; PrimitiveParamTest test = new PrimitiveParamTest(); System.out.println("Initial parameter value is " + parameter); test.changeValue(parameter); System.out.println("After method changeValue call, parameter value is " + parameter); } The value of the formal parameter is displayed. 25 parameter value Output : Initial parameter value is 25 Inside method changeValue, parameter value changed to formal parameter

An Example Using Primitive Data Types public class PrimitiveParamTest { public void changeValue (int parameter) { parameter = 100; System.out.println("Inside method changeValue, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; PrimitiveParamTest test = new PrimitiveParamTest(); System.out.println("Initial parameter value is " + parameter); test.changeValue(parameter); System.out.println("After method changeValue call, parameter value is " + parameter); } As the method is left, the formal parameter is destroyed. 25 parameter value Output : Initial parameter value is 25 Inside method changeValue, parameter value changed to formal parameter

An Example Using Primitive Data Types public class PrimitiveParamTest { public void changeValue (int parameter) { parameter = 100; System.out.println("Inside method changeValue, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; PrimitiveParamTest test = new PrimitiveParamTest(); System.out.println("Initial parameter value is " + parameter); test.changeValue(parameter); System.out.println("After method changeValue call, parameter value is " + parameter); } Control is returned to the calling method. The formal parameter no longer exists. 25 parameter value Output : Initial parameter value is 25 Inside method changeValue, parameter value changed to 100

An Example Using Primitive Data Types public class PrimitiveParamTest { public void changeValue (int parameter) { parameter = 100; System.out.println("Inside method changeValue, parameter value changed to " + parameter); } public static void main (String args[]) { int parameter = 25; PrimitiveParamTest test = new PrimitiveParamTest(); System.out.println("Initial parameter value is " + parameter); test.changeValue(parameter); System.out.println("After method changeValue call, parameter value is " + parameter); } The value of parameter is displayed. 25 parameter value Output : Initial parameter value is 25 Inside method changeValue, parameter value changed to 100 After method changeValue call, parameter value is 25

Passing Objects as Parameters zHow does Java pass objects by value?

Passing Objects as Parameters zHow does Java pass objects by value? zWhen an object is passed to a method, we are actually passing a reference to that object.

Passing Objects as Parameters zHow does Java pass objects by value? zWhen an object is passed to a method, we are actually passing a reference to that object. zThe value that is copied is the address of the object.

Referencing Objects Since a reference to an object actually contains the address of the object, the name of the object can been viewed as a “pointer” to the actual object. Remember that an object doesn’t “exist” until it is instantiated. Shape myObject; myObjec t null

Instantiating Objects The object comes into “existence” when it is instantiated. For example the following code will create the object below: myObject = new Shape(); myObject

Copying Objects When an object is assigned to another object, a copy of the reference to the object is made, not a copy of the actual object. Shape myCopy = myObject; myCopymyObject

Copying Objects In this example, myCopy is an alias for myObject. Both myObject and myCopy refer to the same object. myCopymyObject

Passing Objects as Parameters In the example involving primitive data types, when an int type is passed as a parameter, a copy, known as the formal parameter is used by the called method. When an object is passed to a method, the formal parameter used is an alias, which is a copy of the reference to an object. myObject

Passing Objects as Parameters If the following method is called : foo.someMethod(myObject); A copy or alias of myObject is created and used as the formal parameter. myObject

Passing Objects as Parameters If the following method is called : foo.someMethod(myObject); A copy or alias of myObject is created and used as the formal parameter. myObject The formal parameter

Passing Objects as Parameters If the following method is called : foo.someMethod(myObject); A copy or alias of myObject is created and used as the formal parameter. myObject The formal parameter Inside a method, if the formal parameter calls class methods that modify the state of the object, the object will remain in that state when the program flow returns to the instruction following the method call.

Passing Objects as Parameters myObject Given the following code fragments: public class Foo { public void changeObject(Shape myObject) { myObject.changeShape(); }... }... Foo changer = new Foo(); myObject = new Shape(); changer.changeObject(myObject);

Passing Objects as Parameters myObject Given the following code fragments: public class Foo { public void changeObject(Shape myObject) { myObject.changeShape(); }... }... Foo changer = new Foo(); myObject = new Shape(); changer.changeObject(myObject); When the changeShape method is called, a formal parameter is created.

Passing Objects as Parameters myObject The formal parameter Given the following code fragments: public class Foo { public void changeObject(Shape passedObject) { passedObject.changeShape(); }... }... Foo changer = new Foo(); myObject = new Shape(); changer.changeObject(myObject); When the changeShape method is called, a formal parameter is created. This formal parameter is referred to as passedObject in the changeObject method.

Passing Objects as Parameters myObject The formal parameter Given the following code fragments: public class Foo { public void changeObject(Shape passedObject) { passedObject.changeShape(); }... }... Foo changer = new Foo(); myObject = new Shape(); changer.changeObject(myObject); When passedObject.changeShape() is called, the formal parameter, passedObject, calls the object’s method changeShape().

Passing Objects as Parameters myObject The formal parameter Given the following code fragments: public class Foo { public void changeObject(Shape passedObject) { passedObject.changeShape(); }... }... Foo changer = new Foo(); myObject = new Shape(); changer.changeObject(myObject); This causes the object to change its state (in this case, its shape).

Passing Objects as Parameters myObject The formal parameter Given the following code fragments: public class Foo { public void changeObject(Shape passedObject) { passedObject.changeShape(); }... }... Foo changer = new Foo(); myObject = new Shape(); changer.changeObject(myObject); When the changeObject method ends, the formal parameter ceases to exist.

Passing Objects as Parameters myObject Given the following code fragments: public class Foo { public void changeObject(Shape passedObject) { passedObject.changeShape(); }... }... Foo changer = new Foo(); myObject = new Shape(); changer.changeObject(myObject); When the changeObject method ends, the formal parameter ceases to exist. BUT, the object still exists in its changed state.

Changing object formal parameters If an object formal parameter can change an object’s state by calling the object’s methods, what happens when the value of the formal parameter is changed?

Changing object formal parameters public class Foo { public void changeObject(Shape passedObject) { Shape newObject = new Shape(); passedObject = newObject; passedObject.changeShape(); }... } The code from the previous example is changed slightly.

Changing object formal parameters public class Foo { public void changeObject(Shape passedObject) { Shape newObject = new Shape(); passedObject = newObject; passedObject.changeShape(); }... } If myObject is passed to the changeObject method, myObject

Changing object formal parameters public class Foo { public void changeObject(Shape passedObject) { Shape newObject = new Shape(); passedObject = newObject; passedObject.changeShape(); }... } If myObject is passed to the changeObject method, a formal parameter passedObject is created. myObject passedObject

Changing object formal parameters public class Foo { public void changeObject(Shape passedObject) { Shape newObject = new Shape(); passedObject = newObject; passedObject.changeShape(); }... } Another object, newObject is created. myObject passedObjectnewObject

Changing object formal parameters public class Foo { public void changeObject(Shape passedObject) { Shape newObject = new Shape(); passedObject = newObject; passedObject.changeShape(); }... } Another object, newObject is created. The value of passedObject is changed to newObject. myObject passedObjectnewObject

Changing object formal parameters public class Foo { public void changeObject(Shape passedObject) { Shape newObject = new Shape(); passedObject = newObject; passedObject.changeShape(); }... } The formal parameter passedObject, calls the method changeShape(). myObject passedObjectnewObject

Changing object formal parameters public class Foo { public void changeObject(Shape passedObject) { Shape newObject = new Shape(); passedObject = newObject; passedObject.changeShape(); }... } This causes the object to change its state (in this case, its shape). myObject passedObjectnewObject

Changing object formal parameters public class Foo { public void changeObject(Shape passedObject) { Shape newObject = new Shape(); passedObject = newObject; passedObject.changeShape(); }... } When the changeObject method ends, the formal parameter passedObject ceases to exist. myObject passedObjectnewObject

Changing object formal parameters public class Foo { public void changeObject(Shape passedObject) { Shape newObject = new Shape(); passedObject = newObject; passedObject.changeShape(); }... } When the changeObject method ends, the formal parameter passedObject ceases to exist. myObject remains unchanged. myObject newObject

Summary zJava passes all parameters to a method by value. zThis means that the current value of the actual parameter is copied into the formal parameter in the method header. zSince a reference to an object actually contains the address of the object, the name of the object can been viewed as a “pointer” to the actual object.

Summary zWhen an object is passed to a method, the formal parameter used is an alias, which is a copy of the reference to an object. zAn object’s formal parameter can change an object’s state by calling the class’ methods, but, yIf the value of the formal parameter is changed, the actual object remains unchanged.