References Revisted (Ch 5)

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Road Map Introduction to object oriented programming. Classes
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
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.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Object-Oriented Programming in C++
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Methods We write methods in our programs for many reasons:
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Object Oriented Software Development 4. C# data types, objects and references.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
OOP Basics Classes & Methods (c) IDMS/SQL News
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
Andrew(amwallis) Classes!
Java Memory Management
User-Written Functions
Chapter 7 User-Defined Methods.
Java Memory Management
Static data members Constructors and Destructors
Java Primer 1: Types, Classes and Operators
Testing and Debugging.
Chapter 3: Using Methods, Classes, and Objects
Programming Language Concepts (CIS 635)
CompSci 230 Software Construction
Java Review: Reference Types
More Object Oriented Programming
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.
Lesson 2: Building Blocks of Programming
Java Programming with BlueJ
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Creating Objects in a Few Simple Steps
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
CHAPTER 6 GENERAL-PURPOSE METHODS
Variables Title slide variables.
Functions Pass By Value Pass by Reference
Chapter 4 Topics: class declarations method declarations
Java Programming Language
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Classes and Objects Object Creation
Corresponds with Chapter 5
Coming up Variables Quick look at scope Primitives Objects
CMSC 202 Constructors Version 9/10.
Presentation transcript:

References Revisted (Ch 5) Topics: The difference between reference variables and variables that store primative data types The difference between reference parameters and parameters that store primative data types

Creating a Variable---A Review We create (or define) a variable by declaring its type and its name: int anAge; String aName; anAge The definition of the variable is what puts aside memory for it. Until this happens, you can't refer to it. This is where you get the "undefined variable" type of syntax error. (Point out that, even though you define an attribute like this in a class definition, that does not actually put aside memory for the variable. Memory is not allocated for an object until the object is instantiated from the class. This slide actually applies to variables that are not attributes of objects.) The amount of memory put aside depends on the type of the variable. For an int, it is 32 bits. For a String, it depends on how many characters are in it (String is a class rather than a primitive type, so it has behaviour that includes making itself big enough to hold the characters it has to store). The name of the variable is the symbol we use to refer to that memory location. We don't have to know where it is. Whenever we say anAge in our program, the computer goes to the memory location it has attached that label to, and retrieves the value in there. aName A variable must be defined before it can be used.

Creating a New Object---A Review When we instantiate an object from a class, we use the new operator. new Person(); allocates a block of memory big enough to hold a Person object, and calls the constructor to initialise the values of its attributes. The new operator returns the address of the beginning of that block of memory. The default constructor for the Person class gives the initial values of "" to the name and 0 to the age. If we used the other constructor to create the object, there would be different values in the attribute areas of memory. Show the two constructors in the BlueJ editor.

Naming a New Object---A Review Usually, we want to give an object a name so that we can refer to it afterwards. Person aPerson; puts aside enough memory to hold the address of a Person object, and calls it aPerson. At the moment, it is not the address of any Person object. It is just big enough to hold such an address. Explain what an address is. aPerson

Reference Variables---A Review Person aPerson = new Person(); copies the address of the newly created Person object into the memory location called aPerson. We say that aPerson is a reference to the Person object. It is a reference variable. New person object aPerson From now on, whenever we refer to aPerson in our code, the computer looks in that memory location, finds the address of another memory location, and goes there to get the value you are talking about. Give an example of what the memory address might be, and what the contents of the aPerson location would therefore be. Draw a picture of this happening. This is what happens when you create an instance of a class rather than just a variable of a primitive type such as int, double, boolean. A String variable is also a reference variable, since String is a class. We should, in fact, create a String variable by saying String aName = new String(); but Java has included a short cut that makes it look as if Strings are just primitive data types, because we use them so often.

Variables of Primitive Types vs. Reference Variables A primitive type is one of the built-in basic data types, e.g. int, double, boolean. The name of the variable is a memory location that stores a value. int number = 0; number A reference variable is an object (e.g. a String). The name of the variable is a memory location that stores the address of the object (i.e. the values of its attributes). Person obj Person fred = new Person(); fred

Memory Allocation for Parameters Memory is allocated to the formal parameter at the time the method is called. The memory location is initialised to the actual argument (i.e., the input value). The memory for the formal parameter is deallocated when the method finishes executing. We call this "going out of scope". Draw a picture on the overhead showing how the memory for the parameter comes into existence at the time the method is called, and disappears again when the method terminates. There will be more on scoping later.

Passing Basic Data Types as Parameters If a parameter is of a basic data type, when the formal parameter comes into existence it is a copy of the actual argument. Any change made to the formal parameter in the method will not affect the value in the caller's memory area. This kind of parameter passing is known as call by value. Draw a picture of changing the value of the parameter, while the myAge variable remains unchanged. 25 25 myAge newAge Memory belonging to some other method Memory belonging to setAge

Passing an int to a Method To simplify the code, both calling and called methods are in the same class here. If they are in different classes, you need to ask an object to invoke the method, but otherwise it is the same. The calling method, showing the int before and after the call: public void testIntPassing() { int number = 50; System.out.println(); System.out.println("int before call is " + number); changeNumber(number); System.out.println("int after call is " + number); } Point out where you would have to put the name of an object if the methods were in different classes. Explain what is happening here with all the printlns. Run it in BlueJ.

Changing the Value of the int The called method, showing the int before and after it is changed by this method: private void changeNumber(int aNumber) { System.out.println("int at start of method changeNumber is " + aNumber); aNumber = aNumber + 5; System.out.println("int at end of method changeNumber is " } Bring up BlueJ, and create a TestPerson object. Run its testIntPassing method and show the output in the Java terminal window. Explain that the name of the formal parameter and the actual argument don't have to be the same, because the called method sees only the value anyway. If they are the same, that's fine too. Mention that this technique of printing out values of things to see what is happening is a common one when trying to debug a program. We will avoid looking at the debugger at this stage, as I think it would only confuse most students.

Passing Objects as Parameters When an object is passed as a parameter, what is passed to the method is a copy of the reference to the object. A change to the state of the formal parameter produces a change in the state of the actual argument. Memory for object in calling method Reference to object in calling method object in called method Compare the reference to the object to the key to a house. The method is passed the key, not the house. If you file a bit off the key so it fits a different lock, it might open another house door but not the original one. The value of the variable is passed by value. For an object, the value of the variable is actually the address of the object. The called method gets a copy of this.

Passing an Object to a Method In the calling method, showing the name of the object before and after the call: public void testObjectPassing() { Person friend = new Person("Fred", 20, false); System.out.println(); System.out.println ("Object attribute before call is " + friend.getName()); changeState(friend); System.out.println("Object attribute after call is " } Draw the picture of the object with the attributes in its memory area. The one we will look at here is the name of the Person object.

Changing the State of the Formal Parameter In the called method, showing the name of the formal parameter object at the start and the end (because the formal parameter is an object, it has to be asked to do things): private void changeState(Person someone) { System.out.println("Object attribute at start " + "of method changeState is + someone.getName()); someone.setName(someone.getName().replace('d', 't')); System.out.println("Object attribute at end " + "of method changeState is " } Explain the code, line by line. Put some emphasis on the series of returned values in the call. Run the testObjectPassing method in BlueJ, and show the output on the Java terminal window.