• Instance of class types is reference/object pair – object created using new – accessed through reference name price shares name price shares ibm = new Stock; sun = new Stock; allocate
Trang 1Reference Types
Trang 3class Stock {
.
}
class type
references, not objects
Trang 4• Instance of class types is reference/object pair
– object created using new
– accessed through reference
name price shares
name price shares
ibm = new Stock();
sun = new Stock();
allocate
objects
Trang 5• Assignment of reference types copies only the reference
– does not create new object
name price shares ibm
Stock ibm = new Stock();
Stock s;
s = ibm;
now refer to
same object
Trang 6• Reference may be reassigned
– can refer to different objects at different times
– not required to stay bound to same object
Stock ibm = new Stock();
Stock sun = new Stock();
Trang 7• Parameters of reference type pass only reference
– do not copy object
• Typical to pass reference by value
– but can pass ref or out if needed
void Liquidate(Stock s) {
s
Trang 8compare references?
Comparison
• Two possible meanings for reference comparison
– identity: do the two references refer to the same object?
– equality: do the objects contain equal data?
name IBM price 56.0 shares 100
name Sun price 7.50 shares 200
ibm
sun
compare data?
Trang 9Identity test
• Use library method to guarantee identity test with references
– static method Object.ReferenceEquals
Stock ibm = new Stock();
Stock sun = new Stock();
if (Object.ReferenceEquals(ibm, sun)) .
false, refer to
different objects
Trang 10• Operator== may perform either identity or equality test
– default behavior is identity test
– type can customize behavior by overloading operator
Stock ibm = new Stock();
Stock sun = new Stock();
if (ibm == sun) .
refer to documentation
of Stock class to
determine behavior
Trang 11Keyword null
• Assign null to indicate reference does not refer to object
– invalidates reference
– does not destroy object
Stock ibm = new Stock();
ibm = null;
invalidate
Trang 12Using null reference
• Error to use null reference
– can catch and handle NullReferenceException at runtime
– can test reference before use and avoid exception
test reference before use
Trang 13int y;
.
reference
class Circle {
Trang 14Reference field initial value
• Reference fields default to null
Circle c = new Circle();
Trang 15public Circle(int x, int y, int radius) {
this.center = new Point(x, y) ; this.radius = radius;
} .
}
Circle c = new Circle(1, 2, 3);
x 1
Trang 16• Array declaration gives reference
– does not create array object
bool[] b;
a
b
Trang 18Array assignment
• Array assignment copies only the reference
int[] a = new int[5];
Trang 19Array parameters
• Array parameter passes reference
– does not copy array
• Typical to pass array reference by value
– but can pass ref or out if needed
int Total(int[] a ) {
int total = 0;
foreach (int i in a) total += i;
Trang 20Array of class reference
• Array of class type is array of references
– elements default to null
– must allocate objects with new and store reference in array
Stock[] stocks = new Stock[3];
stocks[0] = new Stock();
stocks[1] = new Stock();
stocks[2] = new Stock();
name price
name price
Trang 21Managed heap
• Memory for reference types obtained from managed heap
– area of memory dedicated for use by the application
– allocation optimized to incur only small runtime overhead
heap
name price shares
name price shares
ibm
sun
Stock ibm = new Stock();
Stock sun = new Stock();
int [] a = new int [10];
bool[] b = new bool[5]; a
b
Trang 22Garbage collection
• Memory of unused objects automatically reclaimed
– not explicitly released by programmer
– called garbage collection
void Method() {
int[] a = new int[5];
Stock s = new Stock();
}
objects no longer
in use, now eligible
to be reclaimed
Trang 23Garbage collection algorithm
• Garbage collector finds unreferenced objects
– begins at root references such as live local variables
– all objects found from some root are still in use
– objects not reachable from a root can be collected
Trang 24Timing of garbage collection
• Garbage collector execution managed by system
– runs as needed
void Allocate() {
int[] a = new int[10000];
Trang 25Control of garbage collection
• GC class provided to give programmer control if needed
– Collect method runs garbage collector
• Should rarely control garbage collection explicitly
– already highly optimized
– collection is expensive, waste of time if done unnecessarily
public class GC {
public static void Collect() { } .
}
force collection
Trang 26Allocation failure
• Heap may eventually run out of available memory
– attempted allocation will fail
– OutOfMemoryException will be generated
– application can catch and handle exception if desired
int[] a = new int[10000];
allocation may fail
if out of memory
Trang 27• Reference/object pair used for class instance and array
• Reference semantics apply in
– assignment
– comparison
– parameter passing
• Garbage collection automatically reclaims unused memory
– relieves programmer of responsibility