cisc 3120 c11: garbage collection and constructors · c11: garbage collection and constructors hui...
TRANSCRIPT
![Page 1: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/1.jpg)
CISC 3120
C11: Garbage Collection and Constructors
Hui Chen
Department of Computer & Information Science
CUNY Brooklyn College
10/5/2017 1CUNY | Brooklyn College
![Page 2: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/2.jpg)
Outline
• Recap
• Project progress and lessons learned
• Memory management
• Java garbage collection
• Constructors
• Assignments
10/5/2017 CUNY | Brooklyn College 2
![Page 3: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/3.jpg)
Memory Management in C++
• In C++: compare the following two.
10/5/2017 CUNY | Brooklyn College 3
Cat *ginger = new Cat();
ginger = nullptr;
Cat *ginger = new Cat();
delete ginger;
ginger = nullptr;
![Page 4: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/4.jpg)
Memory Management in C++
• In C++: compare the following two.
• A programmer must explicitly free the memory allocated to an object in a C++ program; otherwise, the memory cannot be reclaimed and used in the program.
10/5/2017 CUNY | Brooklyn College 4
Cat *ginger = new Cat();
ginger = nullptr;
Cat *ginger = new Cat();
delete ginger;
ginger = nullptr;
![Page 5: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/5.jpg)
Memory Management in Java
• In Java:
• Should a programmer worry about reclaiming the memory?
• Java Garbage Collector takes care of it.
10/5/2017 CUNY | Brooklyn College 5
Cat ginger = new Cat();
ginger = null;
![Page 6: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/6.jpg)
Program Data
• We restrict the definition of program data to data associated with variables
• In C++ and Java, program data are in three categories
• Automatic
• Static
• Dynamic
10/5/2017 CUNY | Brooklyn College 6
![Page 7: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/7.jpg)
Automatic, Static, and Dynamic Program Data
• They differ in
• which region of memory the data reside
• when and how the data is allocated in memory
• when and how the data is deallocated in memory
• Variables
• where: scope
• when: lifetime
10/5/2017 CUNY | Brooklyn College 7
![Page 8: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/8.jpg)
Automatic Data
• Memory is automatically allocated and deallocated for automatic data
• Where: the memory is allocated in a region of memory called stack
• When to allocate: the memory is allocated when execution reaches the scope of the variable
• When to deallocate: the memory is deallocated when execution leaves the scope of the variable
10/5/2017 CUNY | Brooklyn College 8
![Page 9: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/9.jpg)
Automatic Data: Examples
• In C++ and Java: where are the variables for automatic data?
10/5/2017 CUNY | Brooklyn College 9
int sumToNumber(int number) {
int sum = 0;
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
![Page 10: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/10.jpg)
Automatic Data: Examples
• In C++ and Java: where are the variables for automatic data?
• Parameter: number
• Local variables:
• sum
• i
• What are their scopes?
10/5/2017 CUNY | Brooklyn College 10
int sumToNumber(int number) {
int sum = 0;
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
![Page 11: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/11.jpg)
Automatic Data: Examples
• In C++ and Java: where are the variables for automatic data?
• What are their scopes?
10/5/2017 CUNY | Brooklyn College 11
int sumToNumber(int number) {
int sum = 0;
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
numberi
sum
![Page 12: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/12.jpg)
Static Data
• Static data’s existence does not change during the entire execution of a program
• Where: the memory for static data is allocated in a region of memory, generically referred to as the static data segment
• When to allocate:
• the memory is allocated when the program starts,
• or when execution reaches the static variable declaration for the first time
• When to deallocate: the memory for static data is deallocated when the program exits
10/5/2017 CUNY | Brooklyn College 12
![Page 13: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/13.jpg)
Static Data: Example in C++
• In C++: where are the variables for static data?
10/5/2017 CUNY | Brooklyn College 13
int sumToNumber(int number) {
static int sum = 0;
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
![Page 14: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/14.jpg)
Static Data: Example in C++
• In C++: where are the variables for static data?
• Local variable: sum
• What is its scope?
10/5/2017 CUNY | Brooklyn College 14
int sumToNumber(int number) {
static int sum = 0;
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
![Page 15: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/15.jpg)
Static Data: Example in C++
• In C++: where are the variables for static data?
• What are their scopes?
10/5/2017 CUNY | Brooklyn College 15
int sumToNumber(int number) {
static int sum = 0;
for (int i=0; i<number; i++) {
sum += i; }
}
sum
![Page 16: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/16.jpg)
Static and Automatic Data: Example in C++
• In C++: when are they allocated and deallocated?
10/5/2017 CUNY | Brooklyn College 16
int sumToNumber(int number) {
static int sum = 0;
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
int sumToNumber(int number) {
int sum = 0;
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
![Page 17: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/17.jpg)
Static and Automatic Data: Example in C++
• How do the lifetimes of the automatic and static variables differ?
• Compare them in running programs
• When sum is static
• When sum is automatic
10/5/2017 CUNY | Brooklyn College 17
cout << sumToNumber(5) << endl;
cout << sumToNumber(5) << endl;
![Page 18: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/18.jpg)
Static Data in C++
• In C++
• Variables declared as “static”
• Additionally, variables declared outside any function and class body
10/5/2017 CUNY | Brooklyn College 18
![Page 19: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/19.jpg)
Static Data: Example in Java
• Can we write the following in Java?
10/5/2017 CUNY | Brooklyn College 19
int sumToNumber(int number) {
static int sum = 0;
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
![Page 20: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/20.jpg)
Static Data in Java
• Can we write the following in Java?
10/5/2017 CUNY | Brooklyn College 20
int sumToNumber(int number) {
static int sum = 0;
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
![Page 21: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/21.jpg)
Static Data in Java
• Java is more restrictive
• Static variables can only declared within a class, but not within any methods
• Static variables are class variables with the scope of the class
10/5/2017 CUNY | Brooklyn College 21
![Page 22: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/22.jpg)
Static Data: Example in Java
• Where are the static variables?
10/5/2017 CUNY | Brooklyn College 22
class StaticSum {
static int sum = 0;
int sumToNumber(int number) {
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
}
![Page 23: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/23.jpg)
Static Data: Example in Java
• What is its scope?
10/5/2017 CUNY | Brooklyn College 23
class StaticSum {
static int sum = 0;
int sumToNumber(int number) {
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
}
![Page 24: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/24.jpg)
Static Data: Example in Java
• What is the output of this program?
10/5/2017 CUNY | Brooklyn College 24
class StaticSum {
public static void main(String[] args) { StaticSum s = new StaticSum();
System.out.println(s.sumToNumber(5)); System.out.println(s.sumToNumber(5));
}
int sumToNumber(int number) {
for (int i=0; i<number; i++) {
sum += i; }
return sum;
}
static int sum = 0;
}
![Page 25: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/25.jpg)
Static Data in Java
• Java is more restrictive
• Static variables can only declared within a class, but not within any methods
• Static variables are class variables with the scope of the class
• Static variables has globe scope
• Static variables has the lifetime of the program
10/5/2017 CUNY | Brooklyn College 25
![Page 26: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/26.jpg)
Questions?
• Start discussing memory management
• Automatic and static program data
10/5/2017 CUNY | Brooklyn College 26
![Page 27: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/27.jpg)
Dynamic Data
• Programmers are responsible for allocating dynamic data
• In C++: programmers are also responsible for deallocating the dynamic data.
• Where: the memory for static data is allocated in a region of memory, generically referred to as the heap
• When to allocate:
• the memory is allocated when the programmer invokes the “new” operator.
• When to deallocate:
• In Java: when the Java Garbage Collector reclaims the object allocated
• In C++: when the programmer invokes the delete operator to free the memory allocated to the dynamic data
10/5/2017 CUNY | Brooklyn College 27
![Page 28: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/28.jpg)
Dynamic Data in C++ and Java
• In C++, programmers can allocate memory for any data types, i.e., to use “new” operator against any data types
• In Java, programmers can only use “new” operator for reference data types
10/5/2017 CUNY | Brooklyn College 28
![Page 29: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/29.jpg)
Dynamic Data in C++ and Java: Examples
• In Java: which one of the following is legal?
• In C++: which one of the following is legal?
10/5/2017 CUNY | Brooklyn College 29
int i = new int;
int *iPtr = new int;
Cat ginger = new Cat(); int[] iArr = new int[10];
Cat *gingerPtr = new Cat(); int* iArr = new int[10];
![Page 30: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/30.jpg)
Dynamic Data in C++ and Java: Examples
• In Java: which one of the following is legal?
• In C++: which one of the following is legal?
10/5/2017 CUNY | Brooklyn College 30
int i = new int;
int *iPtr = new int;
Cat ginger = new Cat(); int[] iArr = new int[10];
Cat *gingerPtr = new Cat(); int* iArr = new int[10];
![Page 31: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/31.jpg)
Dynamic Data: Java and C++ Comparison• Allocation:
• The same
• Java and C++: dynamic data are created using the new operator
• The different
• In Java: dynamic data can only be objects, cannot be primitive data types.
• In C++: dynamic data can be any data types
• Deallocation
• The different
• In C++: programmers use the delete operator to deallocate memory
• In Java: Java Garbage Collector is responsible for deallocating the memory, programmers have little control.
10/5/2017 CUNY | Brooklyn College 31
![Page 32: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/32.jpg)
Objects in Java and C++
• C++ can have both automatically and dynamically allocated objects
• Java has only dynamically allocated objects
10/5/2017 CUNY | Brooklyn College 32
Cat *ginger = new Cat();
ginger->pounce();
(*ginger).pounce();
Cat ginger;
ginger.pounce();
Cat ginger = new Cat()
ginger.pounce();
Cat ginger;
ginger.pounce();
![Page 33: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/33.jpg)
Objects in Java and C++
• C++ can have both automatically and dynamically allocated objects
• Java has only dynamically allocated objects
10/5/2017 CUNY | Brooklyn College 33
Cat *ginger = new Cat();
ginger->pounce();
(*ginger).pounce();
Cat ginger;
ginger.pounce();
Cat ginger = new Cat()
ginger.pounce();
Cat ginger;
ginger.pounce();
![Page 34: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/34.jpg)
Dynamic Data: Programming Error in C++
• Programmers are responsible for managing dynamic data, which is error-prone
• Common errors
• Inaccessible objects
• Memory leaks
• Dangling pointers
10/5/2017 CUNY | Brooklyn College 34
![Page 35: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/35.jpg)
Java Garbage Collector
• Java is responsible for deallocating dynamic data, and programmers are not.
• In Java, we often write
• In C++, we never write (although it compiles)
10/5/2017 CUNY | Brooklyn College 35
Cat ginger = new Cat();
ginger.pounce(new Animal());
Cat *ginger = new Cat();
ginger->pounce(new Animal());
![Page 36: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/36.jpg)
Different Garbage Collection Algorithms
• How does a Garbage Collector figure out an object is no longer needed and can be deallocated?
• Reference counting
• Trace-based garbage collector
• e.g., Baker’s algorithm
• Copying collector
10/5/2017 CUNY | Brooklyn College 36
![Page 37: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/37.jpg)
Advantage of Garbage Collection
• Avoid bugs, such as,
• Forget to free memory (memory leak)
• Use already freed objects (dangling pointers)
• Also in Java, programmers do not have direct memory access, and cannot accidentally overwrite memory.
10/5/2017 CUNY | Brooklyn College 37
![Page 38: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/38.jpg)
Disadvantage of Garbage Collection
• Consume resources (memory and processor)
• Unpredictable stalls
• Memory leak still possible, but harder to understand
• No manual control
10/5/2017 CUNY | Brooklyn College 38
![Page 39: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/39.jpg)
Questions
• Concept of Garbage Collector
• Programming in Java that does garbage collection
10/5/2017 CUNY | Brooklyn College 39
![Page 40: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/40.jpg)
Constructors
• Like C++, constructors in Java
• have the identical name as the name of the class,
• do not specify return type,
• are called when an object is created,
• and are responsible for initializing the object (instance variables)
10/5/2017 CUNY | Brooklyn College 40
![Page 41: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/41.jpg)
Default Constructor
• Java compiler provides the default constructor when no constructor is written.
10/5/2017 CUNY | Brooklyn College 41
Cat ginger = new Cat(); // calling default constructor
ginger.pounce(new Cat());
class Cat {
void pounce(Cat otherCat) {…}
}
![Page 42: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/42.jpg)
Default Constructor?
• Can we use the default constructor now?
10/5/2017 CUNY | Brooklyn College 42
Cat ginger = new Cat();
ginger.pounce(new Cat(“tiger”));
class Cat {
private String name;
public Cat(String name) {this.name = name;}
void pounce(Cat otherCat) { … }
}
![Page 43: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/43.jpg)
Default Constructor?
• Can we use the default constructor now?
10/5/2017 CUNY | Brooklyn College 43
Cat ginger = new Cat();
ginger.pounce(new Cat(“tiger”));
class Cat {
private String name;
public Cat(String name) {this.name = name;}
void pounce(Cat otherCat) { … }
}
![Page 44: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/44.jpg)
Default and Parameterized Constructors
• Java ceases to create the default constructor
10/5/2017 CUNY | Brooklyn College 44
class Cat {
private String name;
public Cat() {name = “cat”;}
public Cat(String name) {this.name = name;}
void pounce(Cat otherCat) { … }
}
Cat ginger = new Cat(); …
![Page 45: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/45.jpg)
Constructor and Inheritance
• When we create an object of a class, constructors of all super-classes must be called explicitly or implicitly
• Can you name the constructors being called for this example?
10/5/2017 CUNY | Brooklyn College 45
Animal
Feline
Cat PantherPanther brave = new Panther(“brave”);
![Page 46: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/46.jpg)
Calling Super Class’s Constructor Implicitly
• What if we write the constructor as follows,
10/5/2017 CUNY | Brooklyn College 46
class Panther extends Feline {
Color color;
public Panther(String name, Color color) {
this.color = color;
}
public void makeNoise() {…}
}
Animal
Feline
Cat Panther
![Page 47: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/47.jpg)
Calling Super Class’s Constructor Implicitly
• Java compiler will call Feline’s default constructor.
10/5/2017 CUNY | Brooklyn College 47
class Panther extends Feline {
Color color;
public Panther(String name, Color color) {
this.color = color;
}
public void makeNoise() {…}
}
Animal
Feline
Cat Panther
![Page 48: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/48.jpg)
Calling Super Class’s Constructor Explicitly
• Use “super”
10/5/2017 CUNY | Brooklyn College 48
class Panther extends Feline {
Color color;
public Panther(String name, Color color) {
super(name);
this.color = color;
}
public void makeNoise() {…}
}
Animal
Feline
Cat Panther
![Page 49: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/49.jpg)
Recap: Stack and Heap
• Two important region of memories
• Stack
• Heap
10/5/2017 CUNY | Brooklyn College 49
![Page 50: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/50.jpg)
Stack
• Methods are “stacked”
• Stack is organized as stack frames
• A stack frame holds the state of the method (method invocation and automatic data)
• Program counter: which line of code being executed
• Automatic data: values of method parameters and local variables
10/5/2017 CUNY | Brooklyn College 50
![Page 51: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/51.jpg)
Stack: Example
10/5/2017 CUNY | Brooklyn College 51
void doStuff() {
boolean b = true;
go(4);
}
void go(int x) {
int z = x + 24;
crazy();
}
Void crazy() {
int c = 36;
}
doStuff() b
doStuff() b
• doStuff gets called …
go(4) x, z
doStuff() b
go(4) x, z
crazy() c
doStuff() b
go(4) x, z
![Page 52: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/52.jpg)
Heap
• Objects including their instance variables live
10/5/2017 CUNY | Brooklyn College 52
Cat ginger = new Cat() ;
Panther brave = new Panther();The “ginger” object
The “brave” object
ginger brave
Stack Heap
![Page 53: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/53.jpg)
Questions?
• Constructors
• Default constructor
• Overloading constructors
• Inheritance and constructors
• this and super
• Stack and heap
10/5/2017 CUNY | Brooklyn College 53
![Page 54: CISC 3120 C11: Garbage Collection and Constructors · C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017](https://reader033.vdocuments.net/reader033/viewer/2022060305/5f0944007e708231d425ff58/html5/thumbnails/54.jpg)
Assignments
• Project 2
• How is it going?
• Practice assignments
• To be available via CUNY Blackboard
10/5/2017 CUNY | Brooklyn College 54