2. 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
• As soon as the scope ends, all local variables
declared in that scope end
– the variable name and its space are gone
– this happens implicitly – the user has no control over
it
3. 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
• A user maintains a handle on memory
allocated in the heap with a reference
variable
4. 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
– new operator allocates space on the heap
6. Assigning Object References
• Reference can be assigned through new
operator
• obj2 = new Object();
• Reference can assigned to another
reference
• obj2 = obj1;
• WARNING
– when assigning to another reference, both
references now refer to the same object
7. Assigning Object References
• Example
• Object obj1 = new Object();
• Object obj2 = obj1;
main
x = 3
func1
obj1 = 200
obj2 = 200
200
Heap
Stack
Object
8. 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; }
}
9. 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(“f1’s num = “ + f1.getNum()); // prints 10
System.out.println(“f2’s num = “ + f2.getNum()); // prints 10
– need to use the clone() method
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(“f1’s num = “ + f1.getNum()); // prints 5
System.out.println(“f2’s num = “ + f2.getNum()); // prints 10
11. Shallow Clone
• Only copies the fields
– does not copy what the fields reference
• Doesn’t work well for sophisticated objects
• Example:
Class Foo {
private int [] nums;
public void Foo(int size) { nums = new int[size]; }
…
}
Foo f1 = new Foo(5);
Foo f2 = f1.clone();
13. Deep Clone
• Copies fields and what they refer to
• Must reimplement the clone() method
class Foo {
…
public Object clone() {
try {
Foo fobj = (Foo)super.clone(); // copies fields
fobj.nums = (int)nums.clone(); // arrays implement clone
return fobj;
} catch(CloneNotSupportedException e) { }
}
}
14. 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
15. 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
16. 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
– declaring a method abstract
• method must be defined in derived class
17. Abstract Class
abstract class Pixel {
. . .
public abstract void refresh();
}
class ColorPixel extends Pixel {
. . .
public void refresh() {
do some work
}
}
• Note: signature of method in derived class must be
identical to parent declaration of the method
18. 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
19. Interface
• simple example
class Tester implements Foo, Bar {
. . .
}
• Foo and Bar are interfaces
• Tester must define all methods declared in
Foo and Bar
20. Array Review
• Consecutive blocks of memory
• Creation: int [] grades = new int[25];
• __.length: holds size of array
• __.clone(): makes copy of array data
• out-of-bounds exception: trying to access
data outside of array bounds generates an
exception
• Array size is fixed at creation
21. 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
• See Java documentation on-line for
complete details