Download Introduction to Objects and Classes in Java: Lecture 9 from CSMC 131 Spring 2007 - Prof. B and more Study notes Computer Science in PDF only on Docsity!
Lecture 9: CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
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 3.^ Heaps 4.^ Garbage Collection
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
Questions z^ What is
System
in
System.out.println()
z^ Why use
str.equals(“cat”)
to compare
equality of
String str
and^
“cat”
z^ Is the similarity of the notations
System.out.println() str.equals() sc.nextInt() important, or coincidental?
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
Objects z^ Bundles of (related)^ z^ data (“state”)^ z^ operations (“behavior”) z^ Data often referred to as instance variables z^ Operations usually called methods z^ Invoking operations can change state (valuesstored in instance variables)
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
Example of Objects z^ Bank account^ z^ State: account number, type, owner, balance, etc.^ z^ Operations: deposit, withdrawal, transfer, balance inquiry, etc. z^ Student^ z^ State: name, ID, date of birth, major...^ z^ Operations:
lookup grades, lookup class, compute tuition, compute age, … z Scanner z State: stream to read from (e.g
System.in
)
z^ Operations: get an
int,^ double
,^ byte, …
z^ String^ z^ State: sequence of characters^ z^ Operations: comparison test, equality test, (much more), …
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
Accessing State / Methods z^ If^ z^ o
is an object z v is an instance variable of the object z m is a method of the object z^ Then^ z^ o.v
is how to access the data in
v
z^ o.m^
is how to invoke
m
z^ So^ z^ System
is an object, with
out^ an instance variable
z^ out^
is also an object, with
println
a method
z^ System.out.println
is how to access this method!
z^ Suppose
str^ is a String z^ str^
is an object! z^ Methods of this object:
equals
,^ compareTo
, etc.
z^ str.equals
,^ str.compareTo
, etc. invokes these objects
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
Object-Oriented Programming z^ Programs are collections of interactingobjects z^ Writing programs involves identifying whatthe objects should be and programming them z^ Object-oriented languages provide featuresto ease object-oriented programming z^ Defining objects involves indentifying^ z^ state^ z^ methods
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
Classes z^ “Blueprints” (“templates”) for objects z^ Classes include specifications of^ z^ Instance variables (including types, etc.) to include inobjects^ z^ Implementations of methods to include in objects z^ Classes can include other information also, as willbe seen later^ z^ static methods / instance variables^ z^ public / private methods, instance variables^ z^ And so on
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
Student Class Example Conceptually: z^ Instance variables:^ String name^ int ID^ int dateOfBirth^ String major z^ Methods^ getAge^ getGrades^ etc. z^ The actual class implementation will include code for themethods z^ This describes a blueprint for student objects z^ How do we create objects from such a class (blueprint)?
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
Heap = “Fresh Memory” z^ While a program is running, some memory is usedto store variables^ z^ Terminology: stack^ z^ We have been representing stack as table, e.g. z^ Rest of memory is called heap and can be used forother purposes, including storing new objects
Variable
Value x
3 y^
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
AllocatedHeap
Main Memory z^ Stack grows, shrinksduring programexecution (why?) z^ So does “allocatedheap” (part of heap inuse) z^ Unallocated part ofheap is called “free”
Heap Stack
FreeHeap
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
Strings Are Objects z^ Oh really? Where is
new^
in
String name = “Narita”;
z^ Java provides it!^ z^ String
is special because it is used so often z^ Java automatically “fills in”
new
z^ You can too:^ String
name
=^ new
String(“Narita”);
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
In Java, 9 Sorts of Variables z^ 8 primitively typed^ z^ Types are the 8 built-ins (int, byte, double, etc.)^ z^ Storage allocated on stack based on type^ z^ Value stored in stack^ z^ e.g.
int
x z^ Reference typed^ z^ Types are classes^ z^ Storage allocated on stack to hold one memory address(typically, one word)^ z^ What is stored in stack is reference to heap, where actualdata is stored^ z^ e.g.
Scanner
sc^ =
new^
Scanner
(System.in);
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
Heap Issues z^ What happens if
new^ is called and there is no free heap?Crash! z What happens if following areexecuted? String
s; s^ =^ new
String(“cat"); s^ =^ new
String(“dog"); s^ =^ new
String(“cow"); z^ Wasted heap^ z^ “cat”, “dog” no longerreferenced by stack^ z^ Crashes become a problem!
Heap Stack^ s “cat”^ “dog”
“cow”
CMSC 131 Spring 2007Bonnie Dorr (adapted from Rance Cleaveland)
Garbage Collection z^ This “heap management” or “memorymanagement” issue is central in CS z^ Java copes by invoking garbage collector toreclaim unused but still-allocated heap space z^ Garbage collector reclaims memory inallocated heap and returns it to free heap z^ In previous example, “cat” and “dog” wouldbe reclaimed