References (Section 5.2) Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 15-16, 2010 H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 0 / 22
Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 1 / 22
Life Cycle of a Primitive Variable (C/Java) declared and created 1 i n t count ; used and modified 1 count += 1; destroyed automatically (when out of scope) H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 2 / 22
Fun Time (2) What happens in memory? 1 S t r i n g s = " l a l a l a " ; 2 S t r i n g t = " abc " ; 3 S t r i n g a = s + t ; H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 3 / 22
Fun Time (3) What happens in memory? 1 Record r1 ; / / r1. name, r1. score 2 Record r2 ; 3 r1 = new Record ( ) ; 4 r2 = r1 ; / / how many records are there? 5 r1. name = " HTLin " ; 6 r2. score = 98; H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 4 / 22
Fun Time (4) What happens in memory? 1 class Person { S t r i n g name ; Person spouse ; } 2 3 Person George ; 4 Person Marry ; 5 George = new Person ( ) ; 6 George. name = " George " ; 7 Marry = new Person ( ) ; 8 Marry. name = " Marry " ; 9 Mary. spouse = George ; 10 George. spouse = Marry ; H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 5 / 22
Fun Time (5) What happens in memory? 1 class Person { S t r i n g name ; Person spouse ; } 2 3 Person George ; 4 George = new Person ( ) ; 5 George. name = " George " ; 6 George. spouse = new Person ( ) ; 7 George. spouse. name = " Marry " ; 8 George. spouse = new Person ( ) ; 9 George. spouse. name = " Lisa " ; H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 6 / 22
Life Cycle of an Object Instance (Java) reference declared 1 Record r ; instance created 1 r = new Record ( ) ; used and modified 1 System. out. p r i n t l n ( r. name) ; destroyed automatically (when out of use) H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 7 / 22
Reference: Key Point a instance occupies a space in the memory; 老太太住在屏東一個房子裡面 reference (a.k.a. safe pointer): the address to the instance; 用 " 海角七號 " 就可以找到老太太 class-type variable: holds the reference; 一個 " 信封 ", 上面寫著海角七號 any operation on the instance goes thru the reference; 要請老太太 " 回憶 " 時, 拿個信封上寫 " 海角七號 ", 接著寫 " 回憶 ", 阿 Ja 就會使命必達了 老人信封 = new 老人 ( 老太太的身家資料 ); 信封. 回憶 (); H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 8 / 22
null Revisited (1/2) 1 class Record { 2 S t r i n g name ; 3 S t r i n g ID ; 4 i n t score ; 5 } 6 7 p u b l i c class RecordDemo { 8 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 9 Record r1 = new Record ( ) ; 10 System. out. p r i n t l n ( r1. score ) ; 11 System. out. p r i n t l n ( r1. name) ; 12 } 13 } null: Java s reserved word of saying no reference default initial value for extended types (if initialized automatically) 0, NULL, anything equivalent to integer 0: C s way of saying no reference H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 9 / 22
null Revisited (2/2) 1 class Record { 2 S t r i n g name ; 3 S t r i n g ID ; 4 i n t score ; 5 } 6 7 p u b l i c class RecordDemo { 8 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 9 Record r1 = n u l l ; 10 System. out. p r i n t l n ( r1. score ) ; 11 System. out. p r i n t l n ( r1. name) ; 12 } 13 } null pointer exception (run time error): accessing the component of no reference H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 10 / 22
null Revisited: Key Point null: Java s special way of saying no reference H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 11 / 22
Reference Equal (1/2) 1 class Record { 2 S t r i n g name ; 3 i n t score ; 4 } 5 6 p u b l i c class RecordDemo { 7 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 8 Record r1, r2 ; 9 r1 = new Record ( ) ; r2 = new Record ( ) ; 10 r1. name = " HTLin " ; r1. score = 95; 11 r2. name = " HTLin " ; r2. score = 95; 12 System. out. p r i n t l n ( r1 == r2 ) ; 13 r2 = r1 ; 14 System. out. p r i n t l n ( r1 == r2 ) ; 15 } 16 } reference equal: comparison by reference value H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 12 / 22
Reference Equal (2/2) 1 class Record { 2 S t r i n g name ; 3 i n t score ; 4 } 5 6 p u b l i c class RecordDemo { 7 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 8 Record r1, r2 ; 9 r1 = n u l l ; r2 = new Record ( ) ; 10 System. out. p r i n t l n ( r1 == r2 ) ; 11 r2 = r1 ; 12 System. out. p r i n t l n ( r1 == r2 ) ; 13 } 14 } null does not equal non-null... o_o null equals null... O_o H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 13 / 22
Reference Equal: Key Point ==: reference equal rather than content equal for extended types H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 14 / 22
String Equal (1/1) 1 p u b l i c class StringDemo { 2 s t a t i c S t r i n g s1 ; 3 s t a t i c S t r i n g s2 ; 4 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 5 s1 = " HTLin " ; 6 s2 = " HTLin " ; 7 System. out. p r i n t l n ( s1 == s2 ) ; 8 s1 = s1 + " l a l a l a " ; 9 s2 = s2 + " l a l a l a " ; 10 System. out. p r i n t l n ( s1 == s2 ) ; 11 System. out. p r i n t l n ( s1. equals ( s2 ) ) ; 12 } 13 } first true: compiler allocates one constant string only second false: two different string references third true: an action (method) for content comparison H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 15 / 22
String Equal: Key Point String ==: still reference equal, use.equals if want content equal H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 16 / 22
Reference Argument/Parameter (1/3) 1 class Tool { 2 bool t r i c k y ( S t r i n g s1, S t r i n g s2 ) { 3 s2 = s2 + " " ; 4 r e t u r n ( s1 == s2 ) ; 5 } 6 } 7 p u b l i c class Demo{ 8 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 9 Tool t = new Tool ( ) ; 10 S t r i n g sa = " HTLin " ; 11 S t r i n g sb = sa ; 12 System. out. p r i n t l n ( t. t r i c k y ( sa, sb ) ) ; 13 System. out. p r i n t l n ( sa == sb ) ; 14 System. out. p r i n t l n ( t. t r i c k y ( sa + " ", sb ) ) ; 15 } 16 } reference parameter passing: again, value copying sa, sb copied to s1, s2 s2 (reference) changed, sb didn t H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 17 / 22
Reference Argument/Parameter (2/3) 1 class myint { i n t v a l ; myint ( i n t v ) { v a l = v ; } } 2 class Tool { 3 void swap ( myint f i r s t, myint second ) { 4 i n t tmp = f i r s t. v a l ; 5 f i r s t. v a l = second. v a l ; 6 second. v a l = tmp ; 7 System. out. p r i n t l n ( f i r s t. v a l ) ; 8 System. out. p r i n t l n ( second. v a l ) ; 9 } 10 } 11 p u b l i c class Demo{ 12 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 13 Tool t = new Tool ( ) ; 14 myint i = new myint ( 3 ) ; 15 myint j = new myint ( 5 ) ; 16 t. swap ( i, j ) ; 17 System. out. p r i n t l n ( i. v a l ) ; 18 System. out. p r i n t l n ( j. v a l ) ; 19 } 20 } swapped as requested H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 18 / 22
Reference Argument/Parameter (3/3) 1 class myint { i n t v a l ; myint ( i n t v ) { v a l = v ; } } 2 class Tool { 3 void swap ( myint f i r s t, myint second ) { 4 myint tmp = f i r s t ; 5 f i r s t = second ; 6 second = tmp ; 7 System. out. p r i n t l n ( f i r s t. v a l ) ; 8 System. out. p r i n t l n ( second. v a l ) ; 9 } 10 } 11 p u b l i c class Demo{ 12 p u b l i c s t a t i c void main ( S t r i n g [ ] arg ) { 13 Tool t = new Tool ( ) ; 14 myint i = new myint ( 3 ) ; 15 myint j = new myint ( 5 ) ; 16 t. swap ( i, j ) ; 17 System. out. p r i n t l n ( i. v a l ) ; 18 System. out. p r i n t l n ( j. v a l ) ; 19 } 20 } what happens? H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 19 / 22
Reference Argument/Parameter: Key Point argument parameter: by reference copying same for return value H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 20 / 22
this (1/1) 1 class Record { 2 i n t score ; 3 void s e t _ t o ( i n t score ) { t h i s. score = score ; } 4 void adjust_score { t h i s. s e t _ t o ( score +10) ; } 5 } which score? which set_to? this: my (the object s) H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 21 / 22
this: Key Point this: the reference variable pointing to the object itself H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 22 / 22