Methods Again Parameter Passage

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Copyright © Curt Hill Mathematics Functions An example of static methods.
Copyright © 2009 Curt Hill The Picture Object Getting and displaying.
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.
Copyright © Curt Hill Turtles The beginning of media computation.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Copyright © – Curt Hill Pointers A Light Introduction.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CSE 1201 Object Oriented Programming ArrayList 1.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
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];
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
CMSC 202 ArrayList Aug 9, 2007.
Getting and displaying
Functions.
Java Language Basics.
Side Effect Operators Changing the value of variables
More methods, more capabilities
Java Programming: Guided Learning with Early Objects
The for-each or for-all loop introduced in Java 5
Chapter 8 Classes and Objects
Lecture 10: More on Methods and Scope
Methods.
More Object Oriented Programming
CMSC 202 Static Methods.
Methods Additional Topics
Chapter Topics Chapter 5 discusses the following main topics:
March 29th Odds & Ends CS 239.
Doing things more than once
Concepts From Alice Switching to Java Copyright © Curt Hill.
Starting Out with Java: From Control Structures through Objects
Java Programming Language
String Methods: length substring
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
CMSC 202 ArrayList Aug 9, 2007.
Classes and Objects 5th Lecture
Arrays in Java What, why and how Copyright Curt Hill.
Compound Statements A Quick Overview
Java Lesson 36 Mr. Kalmes.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Parameter Passing in Java
CMSC 202 ArrayList Aug 9, 2007.
Other displays Saving Arrays Using fors to process
Multiple Inheritance in C++
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Java Classes and Objects 4th Lecture
Object Oriented Programming Review
Functions, Parameters and Scope
Classes and Objects Static Methods
Classes, Objects and Methods
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
CIS 110: Introduction to Computer Programming
The IF Revisited A few more things Copyright © Curt Hill.
Classes and Objects Object Creation
The beginning of media computation Followed by a demo
Methods Scope How are names handled?
Methods Coding in Java Copyright © Curt Hill.
CMSC 202 Constructors Version 9/10.
C Parameter Passing.
Presentation transcript:

Methods Again Parameter Passage Copyright © 1997 - 2009 Curt Hill

The Handle Scheme Objects are referred to by a handle They are not valid until a value is assigned to them Either from An existing object A use of new The use of an existing object makes another handle that points at the same object Copyright © 1997 - 2009 Curt Hill

Recall Triangle The last version of the triangle had two turtles They moved independently Consider the next screens Copyright © 1997 - 2009 Curt Hill

Example static void triangle( Turtle t, int len){ t.forward(len); t.turn(120); } Copyright © 1997 - 2009 Curt Hill

Called by public static void main( String [] a){ World w = new World(400,240); Turtle yertle = new Turtle(w); yertle.setPenWidth(3); triangle(yertle,80); Turtle sam=new Turtle(100,100,w); sam.setPenWidth(4); triangle(sam,40); } Copyright © 1997 - 2009 Curt Hill

Gives Results Copyright © 1997 - 2009 Curt Hill

Independence The two turtles move independently since they are two objects We will get a different result if we assign one to the other instead of creating a second Copyright © 1997 - 2009 Curt Hill

Called by public static void main( String [] a){ World w = new World(400,240); Turtle yertle = new Turtle(w); yertle.setPenWidth(3); triangle(yertle,80); Turtle sam = yertle; sam.setPenWidth(4); triangle(sam,40); } Copyright © 1997 - 2009 Curt Hill

Gives Results Copyright © 1997 - 2009 Curt Hill

What was changed? The middle statement Turtle sam=new Turtle(100,100,w); Was changed to Turtle sam = yertle; Instead of sam being independent, a new turtle, it is a handle to the same old turtle Copyright © 1997 - 2009 Curt Hill

The difference Turtle yertle = new Turtle(w); Turtle instance yertle Turtle sam = new Turtle(100,100,w); sam Turtle instance Turtle yertle = new Turtle(w); Turtle instance yertle Turtle sam = yertle; sam Copyright © 1997 - 2009 Curt Hill

Another Example public static void main( String [] a){ World w = new World(400,240); Turtle yertle = new Turtle(w); yertle.setPenWidth(3); Turtle sam = yertle; sam.forward(50); yertle.turn(90); sam.turn(45); yertle.forward(90); } Copyright © 1997 - 2009 Curt Hill

Results Copyright © 1997 - 2009 Curt Hill

Commentary It is just one turtle in this case yertle and sam are synonyms or aliases for exactly the same object Any method on sam affects the common object which yertle also points at Once we understand this we can understand parameter passage Copyright © 1997 - 2009 Curt Hill

Parameter Passage Mechanisms How are parameters passed in Java? In general there are several ways to do this Java always passes parameters by value This just means a copy is made and given to the method This has a different effect for primitives and objects Copyright © 1997 - 2009 Curt Hill

Passing Primitives Call by value is the way a primitive is passed to a method A copy of the primitive is made and given to the method If the method changes the primitive, the change is made to the copy, not to the original Thus the actual parameter is never changed Consider triangle again Copyright © 1997 - 2009 Curt Hill

Triangle Changed static void triangle( Turtle t, int len){ t.forward(len); t.turn(120); len = 10; // Set len } Copyright © 1997 - 2009 Curt Hill

Called by public static void main(String [] a){ World w = new World(400,240); Turtle yertle = new Turtle(w); yertle.setPenWidth(3); Turtle sam = new Turtle(100,100,w); sam.setPenWidth(4); int dist = 80; triangle(yertle,dist); triangle(sam,dist); System.out.println(dist); } Copyright © 1997 - 2009 Curt Hill

Gives Results Copyright © 1997 - 2009 Curt Hill

Commentary Even though triangle changed the value of its parameter it had no effect on the outside It changed a copy, rather than the original It works a little different with objects As we have and will see Copyright © 1997 - 2009 Curt Hill

Passing Objects When an object is passed as a parameter a copy of the handle is passed As we saw with assignment we then have two handles pointing at the same object These are synonyms or aliases Copyright © 1997 - 2009 Curt Hill

Triangle revisited When the triangle method was called it moved the turtle and changed its direction The copy of the handle still pointed at the same object Unlike passing a primitive (like int) we can actually change the object Copyright © 1997 - 2009 Curt Hill

Finally The net effect is that we can change the characteristics of a passed object In the turtle: direction, position etc. A copy of a handle still points at the object A primitive cannot be changed in any significant way Copyright © 1997 - 2009 Curt Hill