DOC PREVIEW
UMD CMSC 131 - Lecture 9: Introduction to Objects and Classes

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 9 Introduction to Objects and Classes Last time 1 Precedence and short circuiting 2 Type casting 3 Software lifecycle Today 1 Pseudo code from last lecture 2 Objects and classes 9 20 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Questions What is System in System out println Why use str equals cat to compare equality of String str and cat Is the similarity of the notations System out println str equals sc nextInt important or coincidental CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Answers will come but are based on fact that Java is an object oriented OO language Other OO languages C Smalltalk Eiffel What is an object What does it mean for a language to be object oriented CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 Objects Bundles of related data state operations behavior Data often referred to as instance variables Operations usually called methods Invoking operations can change state values stored in instance variables CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Example of Objects Bank account State account number type owner balance etc Operations deposit withdrawal transfer balance inquiry etc Student State name ID date of birth major Operations lookup grades lookup class compute tuition compute age Scanner State stream to read from e g System in Operations get an int double byte String State sequence of characters Operations comparison test equality test much more CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Sample Student Object State Name ID Kerry Keenan 444230695 DOB 06 22 1987 Major CMSC Methods getAge getGrades date age semester grades etc etc CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Accessing State Methods If Then o v is how to access the data in v o m is how to invoke m So o is an object v is an instance variable of the object m is a method of the object System is an object with out an instance variable out is also an object with println a method System out prinln is how to access this method Suppose str is a String str is an object Methods of this object equals compareTo etc str equals str compareTo etc invokes these objects CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 Object Oriented Programming Programs are collections of interacting objects Writing programs involves identifying what the objects should be and programming them Object oriented languages provide features to ease object oriented programming Defining objects involves indentifying state methods CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 More Questions How do you create objects Do objects have types Answer classes CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 Classes Blueprints templates for objects Classes include specifications of Instance variables including types etc to include in objects Implementations of methods to include in objects Classes can include other information also as will be seen later static methods instance variables public private methods instance variables And so on CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Student Class Example Conceptually Instance variables String name int ID int dateOfBirth String major Methods getAge getGrades etc The actual class implementation will include code for the methods This describes a blueprint for student objects How do we create objects from such a class blueprint CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 How Are Objects Created In Java using new Recall Scanner sc new Scanner System in Invoking new creates fresh copies of instance variables in the heap returns the address where the fresh variables are stored Heap Address CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Heap Fresh Memory While a program is running some memory is used to store variables Terminology stack We have been representing stack as table e g Variable x Value 3 y 4 5 Rest of memory is called heap and can be used for other purposes including storing new objects CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 Main Memory Stack grows shrinks during program execution why So does allocated heap part of heap in use Unallocated part of heap is called free Heap Allocated Heap CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland Free Heap Stack 13 Object Creation New space allocated in heap to store instance variables Reference address to this space is returned Heap Allocated Heap Scanner sc new Free Heap Stack sc CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 14 Strings Are Objects Oh really Where is new in String name Narita Java provides it String is special because it is used so often Java automatically fills in new You can too String name new String Narita CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 15 In Java 9 Sorts of Variables 8 primitively typed Types are the 8 built ins int byte double etc Storage allocated on stack based on type Value stored in stack e g int x Reference typed Types are classes Storage allocated on stack to hold one memory address typically one word What is stored in stack is reference to heap where actual data is stored e g Scanner sc new Scanner System in CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 16 Example int x 7 float y 3 3 String f cat Stack x Heap 7 y 3 3 f cat CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 17 Heap Issues What happens if new is called and there is no free heap Crash What happens if following are executed String s s new String cat s new String dog s new String cow Wasted heap cat dog no longer referenced by stack Crashes become a problem Stack Heap s cat dog cow CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 18 Garbage Collection This heap management or memory management issue is central in CS Java copes by invoking garbage collector to reclaim unused but still allocated heap space Garbage collector reclaims memory in allocated heap and returns it to free heap In previous example cat and dog would be reclaimed CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 19


View Full Document

UMD CMSC 131 - Lecture 9: Introduction to Objects and Classes

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 9: Introduction to Objects and Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 9: Introduction to Objects and Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 9: Introduction to Objects and Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?