TOPIC 13 OBJECTS IN MEMORY, PASSING PARAMETERS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial.

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
1. 2 Introduction to Methods  Method Calls  Parameters  Return Type  Method Overloading  Accessor & Mutator Methods  Student Class: Revisited.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
FIT Objectives By the end of this lecture, students should: understand how parameters are passed to and from methods understand what the differences.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
04-Intro-Object-Oriented-In-Java1 Barb Ericson Georgia Institute of Technology Sept 2009 Introduction to Object-Oriented Programming in Java.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Introduction to Programming Writing Java Beginning Java Programs.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
Methods in Java CSC1401. Overview In this session, we are going to see how to create class-level methods in Java.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Writing Methods Mrs. C. Furman October 9, Drawing a Square World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); turtle1.forward.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
Sadegh Aliakbary Sharif University of Technology Spring 2011.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
CSC 142 Computer Science II Zhen Jiang West Chester University
Introduction to Programming Writing Java Beginning Java Programs.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
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.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University.
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.
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
AKA the birth, life, and death of variables.
Intro To Classes Review
Chapter 10: Void Functions
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.
More Object Oriented Programming
An Introduction to Java – Part II
Chapter 4 (part 2).
Parameter Passing in Java
Teaching Java using Turtles part 3
Barb Ericson Georgia Institute of Technology Oct 2005
Simulating Reference Parameters in C
Object Oriented Programming Review
Barb Ericson Georgia Institute of Technology June 2005
Defining Classes and Methods
Classes, Objects and Methods
Barb Ericson Georgia Institute of Technology Oct 2005
References Revisted (Ch 5)
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
C Parameter Passing.
Presentation transcript:

TOPIC 13 OBJECTS IN MEMORY, PASSING PARAMETERS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared by B. Ericson.

Outline 2  How objects are stored in memory  How parameters are passed to methods

Objects in Memory 3  Recall the Student class with two constructors defined as follows: public Student(String theName) { this.name = theName; } public Student(String theName, double[] grades) { this.name = theName; this.gradeArray = grades; }

Objects in Memory 4  Here is what happens in memory when the following code segment is executed String name1 = “Student 1”; String name2 = “Student 2”; double [] grades1 = {100.0, 90.0,60.0}; Student student1 = new Student(name1,grades1); Student student2 = new Student(name2);

5 student1 student2 name gradeArray Student name gradeArray Student “ Student 2 ” “Student 1” null name2 name1 grades1 Trace

6  Recall that a reference variable “points to” an object, i.e. it contains the “location” of the object  So, for student1  The name field contains the location of the string pointed to by name1  The gradeArray field contains the location of the array pointed to by grades1  For student2, gradeArray is null

Objects in Memory 7  Now consider the following code segment instead String name1 = “Student 1”; String name2 = “Student 2”; double [] grades1 = {100.0, 90.0,60.0}; Student student1 = new Student(name1,grades1); Student student2 = new Student(name2,grades1);

8 student1 student2 name gradeArray Student name gradeArray Student “ Student 2 ” name2 name1 grades1 Trace “Student 1”

Objects in Memory 9  The gradeArray variable for student1 and student2 both point to the same object  Now consider the following method defined in the Student class, to change a student’s grades: public void changeGrades(double [] newGradeArray) { for (int i = 0; i < newGradeArray.length; i++) this.gradeArray[i] = newGradeArray[i]; }  We call double [] newGrades = {100.0,100.0,100.0}; student1.changeGrades(newGrades);

10 student1 student2 name gradeArray Student name gradeArray Student “ Student 2 ” name2 name1 grades1 Trace “Student 1”

11 student1 student2 name gradeArray Student name gradeArray Student “ Student 2 ” name2 name1 grades grades2 A better situation “Student 1”

Conclusion 12  It is important to know which objects are being referenced by which reference variables.  Diagrams help.

Passing Parameters 13  You have been passing parameters to methods since you learned about the Turtle class  Examples: World world1 = new World(); Turtle turtle1 = new Turtle(100,200,world1); turtle1.forward(50); turtle1.turn(-90); int howFar = 55; turtle1.forward(howFar);

Formal and actual Parameters 14  Example: recall the method we defined for the Turtle class: public void drawSquare(int width){ … }  The variable in the parameter list in the method definition is known as a formal parameter  When we invoke a method with a parameter, that is known as an actual parameter, for example: turtle1.drawSquare(5);  When the drawSquare method is executed, the value of the actual parameter size is copied to the formal parameter width

Passing Parameters: a Trace 15 In the calling program: int size = 50; World world1 = new World(); Turtle turtle1 = new Turtle(w); turtle1.drawSquare(size); world1 World Object turtle1 Turtle Object size 50

Passing Parameters: a Trace 16 In drawSquare: The value of the actual parameter size was copied to the parameter variable width public void drawSquare(int width) { this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); } width 50 this turtle1 Turtle Object world1 World Object In drawSquare() In main()

Passing Parameters 17  Remember: the value of an actual parameter is copied to the parameter variable when the method starts executing  In our example: the method drawSquare does not have any access to the actual variable size in the main method

Reference Variables as Parameters 18  What happens when parameters are reference variables?  We have seen several examples in the Picture class: public void copyPicture (Picture pic) public void copyPictureTo(Picture pic, int xStart, int yStart)  What are the formal parameters in each of these method definitions?

Reference Variables as Parameters 19  Consider a program containing the following code segment: Picture pic1 = new Picture(…); Picture pic2 = new Picture(…); pic2.copyPicture(pic1);  What happens when the copyPicture method is executed? pic1 is the actual parameter

Reference Variables as Parameters 20 pic1 pic2 Picture object In copyPicture pic this actual parameter formal parameter

Reference Variables as Parameters 21  Recall that a reference variable “points to” an object, i.e. it contains the location of the object  In Java, the value of an actual parameter is copied to the formal parameter when the method starts executing  The contents of actual parameter pic1 is copied to the formal parameter pic  So pic now contains the location of (points to) the same object as pic1

Reference Variables as Parameters 22  As with parameters of primitive types, a method cannot change the contents of the actual parameter  In our example: the method copyPicture does not have access to the actual variable pic1 in the main method  However, it can change the data in the object that the actual parameter points to  Why? Because the formal parameter variable points to the same object  This is called a side-effect  It’s usually safer to avoid them

23 public void modify(Picture pic) { for (int x=0; x<pic.getWidth(); x++) for (int y=0; y<pic.getHeight(); y++) { Pixel pix1 = pic.getPixel(x, y); Pixel pix2 = this.getPixel(x, y); pix1.setColor(pix2.getColor()); } A side effect

24 pic1 pic2 Picture object In modify pic this Reference variables as parameters

Summary 25  How objects are stored in memory  Reference variables  Tracing  How parameters are passed to methods