Advanced Java Programming: CS 537 - Data Structures and Algorithms
Advanced Java Programming: CS 537 - Data Structures and Algorithms
Advanced Java Programming: CS 537 - Data Structures and Algorithms
The Stack
The stack is the place where all local variables
are stored
a local variable is declared in some scope
Example
int x; // creates the variable x on the stack
The Heap
The heap is an area of memory that the
user handles explicitly
user requests memory through new operator
java does garbage collection to reclaim
unused memory
Creating Objects
All objects are created on the heap
A reference to object is stored on the stack
simply declaring an object does not create it
automatically set to null
Creating Objects
Example
Object obj1 = new Object();
Object obj2;
func1
obj1 = 200
obj2 = null
Object
200
main
x=3
Stack
Heap
WARNING
when assigning to another reference, both
references now refer to the same object
func1
obj1 = 200
obj2 = 200
Object
200
main
x=3
Stack
Heap
Simple Class
class Foo implements Cloneable {
private int num;
public void Foo(int num) { this.num = num; }
public void setNum(int num) { this.num = num; }
public int getNum() { return num; }
}
Copying an Object
Want to create and modify copy of object
remember, simple assignment not enough
Foo f1 = new Foo(5);
Foo f2 = f1; // still only one object 2 references
f2.setNum(10);
System.out.println(f1s num = + f1.getNum()); // prints 10
System.out.println(f2s num = + f2.getNum()); // prints 10
clone() Method
To use clone() must implement Cloneable
Object.clone() is automatically inherited by every
class
by default, it creates a new object and copies all fields
Example
Foo f1 = new Foo(5);
Foo f2 = f1.clone();
f2.setNum(10);
System.out.println(f1s num = + f1.getNum()); // prints 5
System.out.println(f2s num = + f2.getNum()); // prints 10
Shallow Clone
Only copies the fields
does not copy what the fields reference
}
Foo f1 = new Foo(5);
Foo f2 = f1.clone();
Shallow Clone
Foo f1 = new Foo(5);
Foo f2 = f1.clone();
func1
f1 = 200
f2 = 100
nums = 50
200
Array
nums = 50
100
Stack
Heap
50
Deep Clone
Copies fields and what they refer to
Must reimplement the clone() method
class Foo {
Inheritance
lets one class inherit fields and methods
from another class
use keyword extends to explicitly inherit
another classes public and protected
fields/methods
can only explicitly extend from one class
all classes implicitly extend the Object
class
Inheritance
overriding a method
must have the same signature as original
declaring a method final means future derived
classes cannot override the method
overloading a method
method has same name but different
signature
they are actually different methods
Inheritance
abstract classes and methods
declaring a class abstract
must have an abstract method
class cannot be directly used to create an object
class must be inherited to be used
Abstract Class
abstract class Pixel {
...
public abstract void refresh();
}
class ColorPixel extends Pixel {
...
public void refresh() {
do some work
}
}
Interface
basically an abstract class where all
methods are abstract
cannot use an interface to create an object
class that uses an interface must
implement all of the interfaces methods
use the implements keyword
a class can implement more than one
interface
Interface
simple example
class Tester implements Foo, Bar {
...
}
Array Review
Vector Class
Very similar to an array
Major difference: vectors can grow beyond
original size
if a certain capacity is exceeded, a new, larger
memory region is allocated for vector
copy all data to the new area