be concrete about abstractions - 國立臺灣大學htlin/course/oop09spring/...static revisited and...

Post on 10-Oct-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Be Concrete about Abstractions

Hsuan-Tien Lin

Deptartment of CSIE, NTU

OOP Class, March 24, 2009

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 0 / 55

Object/Class Lifecycle

Object Lifecycle (1/1)

1 c lass Record {2 i n t score ;3 Record ( i n t i n i t _ s c o r e ) { score = i n i t _ s c o r e ; }4 pro tec ted vo id f i n a l i z e ( ) throws Throwable { }5 }6 p u b l i c c lass RecordDemo {7 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {8 Record r ; / / re ference declared9 Record r2 ; / / re ference declared

10 r = new Record ( 6 0 ) ; / / memory a l l o c a t e d (RHS)11 / / and cons t ruc to r c a l l e d12 / / re ference assigned (LHS)13 r2 = r ; / / re ference copied14 r . score = 3; / / ob jec t content accessed15 r . show_score ( ) ; / / ob jec t ac t i on performed16 r2 = n u l l ; r = n u l l ; / / memory s l o t i s o l a t e d17 / / . . . .18 / / f i n a l i z e r c a l l e d19 / / or JVM terminated20 }21 }

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 1 / 55

Object/Class Lifecycle

Object Lifecycle: Key Point

we control birth, life, death, funeral design, but not theexact funeral time (nor whether it would happen)

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 2 / 55

Object/Class Lifecycle

Class Lifecycle (1/1)

1 c lass Record {2 s t a t i c { / / c lass i n i t i a l i z e r3 count = 0 ; / / a c t u a l l y , done by d e f a u l t4 }5 s t a t i c i n t count ;6 i n t score ;7 Record ( i n t i n i t _ s c o r e ) { score = i n i t _ s c o r e ; count ++; }8 pro tec ted vo id f i n a l i z e ( ) throws Throwable { }9 / / c l a s s F i n a l i z e ? has been removed

10 }11 / / the c lass RecordDemo should be loaded a f t e r ’ java RecordDemo ’12 p u b l i c c lass RecordDemo {13 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {14 / / the c lass Record should be loaded ( i n i t i a l i z e d ) before15 / / the f o l l o w i n g l i n e s16 Record r , r2 ;17 r = new Record ( 6 0 ) ;18 r2 = r ;19 / / . . . .20 / / by de fau l t , no unload ( and hence count would remain )21 / / i f unloaded , no guarantees22 }23 }

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 3 / 55

Object/Class Lifecycle

Class Lifecycle: Key Point

automatic and dynamic loading/linking in JVM:almost no need to care about load/unload

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 4 / 55

Methods: the Basic

Method (1/2, Callee’s View)

1 c lass Record {2 i n t score ;3 vo id add_to ( i n t i nc ) { score += inc ; }4 double get_adjusted ( ) { r e t u r n ( double ) ( score + 30 ) ; }5 }

method: what I (the object) doparameter: what I get from the callerreturn value: what I want to tell the caller

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 5 / 55

Methods: the Basic

Method (2/2, Caller’s View)

1 c lass Record {2 i n t score ;3 vo id add_to ( i n t i nc ) { score += inc ; }4 double get_adjusted ( ) { r e t u r n ( double ) ( score + 30 ) ; }5 }6 p u b l i c c lass RecordDemo {7 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {8 Record r = new Record ( ) ;9 r . addto ( 5 0 ) ;

10 System . out . p r i n t l n ( r . get_ad justed ( ) ) ;11 }12 }

method: what I (caller) want the object to doargument: what I tell the calleereturn value: what I want to hear from the callee

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 6 / 55

Methods: the Basic

Method: Key Point

method: an abstraction of action, where informa-tion is passed through argument/parameter and re-turn values

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 7 / 55

Methods: Value Passing

Return Values (1/1)

1 c lass Record {2 S t r i n g name ;3 i n t score ;4 vo id add_to ( i n t i nc ) { score += inc ; }5 double get_adjusted ( ) { r e t u r n ( score + 30 ) ; }6 S t r i n g get_name ( ) { r e t u r n name ; }7 }8 p u b l i c c lass RecordDemo {9 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {

10 Record r = new Record ( ) ;11 r . name = " HTLin " ;12 System . out . p r i n t l n ( r . get_name ( ) == r . name ) ;13 }14 }

void: no returnprimitive type: its valuereference type: its value (a.k.a. reference, not content)

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 8 / 55

htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆

Methods: Value Passing

Return Values: Key Point

Java: return by primitive/reference values

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 9 / 55

Methods: Value Passing

Primitive Argument/Parameter (1/1)

1 c lass Tool {2 vo id swap ( i n t f i r s t , i n t second ) {3 i n t tmp = f i r s t ;4 f i r s t = second ;5 second = tmp ;6 System . out . p r i n t l n ( f i r s t ) ;7 System . out . p r i n t l n ( second ) ;8 }9 }

10 p u b l i c c lass Demo{11 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {12 Tool t = new Tool ( ) ;13 i n t i = 3 ; i n t j = 5 ;14 t . swap ( i , j ) ;15 System . out . p r i n t l n ( i ) ;16 System . out . p r i n t l n ( j ) ;17 }18 }

first, second: swappedi, j: didn’t

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 10 / 55

Methods: Value Passing

Primitive Argument/Parameter: Key Point

argument⇒ parameter: by value copying–change in parameter does not change argument

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 11 / 55

Methods: Value Passing

Reference Argument/Parameter (1/3)

1 c lass 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 c lass Demo{8 p u b l i c s t a t i c vo id 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 copyingsa, sb copied to s1, s2s2 (reference) changed, sb didn’t

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 12 / 55

htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆

Methods: Value Passing

Reference Argument/Parameter (2/3)

1 c lass myInt { i n t va l ; myInt ( i n t v ) { va l = v ; } }2 c lass Tool {3 vo id swap ( myInt f i r s t , myInt second ) {4 i n t tmp = f i r s t . va l ;5 f i r s t . va l = second . va l ;6 second . va l = tmp ;7 System . out . p r i n t l n ( f i r s t . va l ) ;8 System . out . p r i n t l n ( second . va l ) ;9 }

10 }11 p u b l i c c lass Demo{12 p u b l i c s t a t i c vo id 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 . va l ) ;18 System . out . p r i n t l n ( j . va l ) ;19 }20 }

swapped as requestedH.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 13 / 55

htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
群組
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
群組
htlin
鉛筆
htlin
群組
htlin
群組
htlin
鉛筆

Methods: Value Passing

Reference Argument/Parameter (3/3)

1 c lass myInt { i n t va l ; myInt ( i n t v ) { va l = v ; } }2 c lass Tool {3 vo id 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 . va l ) ;8 System . out . p r i n t l n ( second . va l ) ;9 }

10 }11 p u b l i c c lass Demo{12 p u b l i c s t a t i c vo id 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 . va l ) ;18 System . out . p r i n t l n ( j . va l ) ;19 }20 }

what happens?H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 14 / 55

htlin
鉛筆
htlin
群組
htlin
群組
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆

Methods: Value Passing

Reference Argument/Parameter: Key Point

argument⇒ parameter: by reference copying–value change: does not change argument–content change: change the “object”

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 15 / 55

Methods: Value Passing

Array Argument/Parameter (1/1)

1 c lass Tool {2 vo id swap ( i n t [ ] both ) {3 i n t tmp = both [ 0 ] ;4 both [ 0 ] = both [ 1 ] ;5 both [ 1 ] = tmp ;6 }7 }8 p u b l i c c lass Demo{9 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {

10 Tool t = new Tool ( ) ;11 i n t [ ] a r r = new i n t [ 2 ] ;12 a r r [ 0 ] = 3 ; a r r [ 1 ] = 5 ;13 t . swap ( a r r ) ;14 System . out . p r i n t l n ( a r r [ 0 ] ) ;15 System . out . p r i n t l n ( a r r [ 1 ] ) ;16 }17 }

array is just special reference, same calling mechanism

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 16 / 55

Methods: Value Passing

Array Argument/Parameter: Key Point

argument⇒ parameter: by reference value copying–same as reference argument/parameter

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 17 / 55

Static Revisited and Beyond

Static Variables Revisited (1/1)

1 c lass Record {2 s t a t i c i n t t o t a l _ r e c = 0;3 i n t i d ;4 p u b l i c Record ( ) { i d = t o t a l _ r e c ++; }5 }6 p u b l i c c lass RecordDemo {7 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {8 Record r1 = new Record ( ) ;9 Record r2 = n u l l ;

10 Record r3 = new Record ( ) ;11 System . out . p r i n t l n ( r1 . t o t a l _ r e c ) ;12 System . out . p r i n t l n ( r2 . t o t a l _ r e c ) ;13 System . out . p r i n t l n ( Record . t o t a l _ r e c ) ;14 System . out . p r i n t l n ( r1 . i d ) ;15 System . out . p r i n t l n ( r2 . i d ) ;16 System . out . p r i n t l n ( Record . i d ) ;17 }18 }

r2.total_rec⇒ Record.total_rec in compile time

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 18 / 55

htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
鉛筆
htlin
群組
htlin
鉛筆

Static Revisited and Beyond

Static Variables Revisited: Key Point

static variable:of the class (shared), not of an object

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 19 / 55

Static Revisited and Beyond

Static Methods (1/2)

1 c lass myMath {2 double mean( double a , double b ) {3 r e t u r n ( a + b ) ∗ 0 . 5 ;4 }5 }6 p u b l i c c lass MathDemo{7 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {8 double i = 3 . 5 ;9 double j = 2 . 4 ;

10 myMath m = new MyMath ( ) ;11 System . out . p r i n t l n (m. mean( i , j ) ) ;12 }13 }

new a myMath object just for computing mean–lazy people don’t want to do so

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 20 / 55

Static Revisited and Beyond

Static Methods (2/2)

1 c lass myMath {2 s t a t i c double mean( double a , double b ) {3 r e t u r n ( a + b ) ∗ 0 . 5 ;4 }5 }6 p u b l i c c lass MathDemo{7 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {8 double i = 3 . 5 ;9 double j = 2 . 4 ;

10 System . out . p r i n t l n (myMath . mean( i , j ) ) ;11 System . out . p r i n t l n ( ( new myMath ( ) ) . mean( i , j ) ) ;12 }13 }

make the method a static (class) one–no need to new an objectsimilar to static variable usage

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 21 / 55

Static Revisited and Beyond

Static Methods: Key Point

static method:associated with the class,no need to create an object

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 22 / 55

Static Revisited and Beyond

Use of Static Methods (1/3)

1 p u b l i c c lass UtilDemo {2 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {3 System . out . p r i n t l n ( Math . PI ) ;4 System . out . p r i n t l n ( Math . s q r t ( 2 . 0 ) ) ;5 System . out . p r i n t l n ( Math .max( 3 . 0 , 5 . 0 ) ) ;6 System . out . p r i n t l n ( I n tege r . t o B i n a r y S t r i n g ( 1 5 ) ) ;7 }8 }

commonly used as utility functions(so don’t need to create object)

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 23 / 55

Static Revisited and Beyond

Use of Static Methods (2/3)

1 c lass Record {2 s t a t i c i n t t o t a l _ r e c = 0;3 Record ( ) { t o t a l _ r e c ++; }4 s t a t i c vo id show_tota l_rec ( ) {5 System . out . p r i n t l n ( t o t a l _ r e c ) ;6 }7 }8 p u b l i c c lass RecordDemo {9 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {

10 Record r1 = new Record ( ) ;11 Record . show_tota l_rec ( ) ;12 }13 }

class related actions rather than object related actions

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 24 / 55

Static Revisited and Beyond

Use of Static Methods (3/3)

1 p u b l i c c lass MainDemo{2 s t a t i c vo id p r i n t S t r ( S t r i n g s ) {3 System . out . p r i n t ( s ) ;4 }5 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {6 p r i n t S t r ( " I am main ; I am also s t a t i c " ) ;7 }8 }

main is static (called by classname during ’java className’)as tools for other static methods

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 25 / 55

Static Revisited and Beyond

Use of Static Methods: Key Point

static method:compile time determinedper classsometimes useful

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 26 / 55

Instance versus Class

Instance versus Class (1/2)

1 c lass Record {2 s t a t i c i n t count ;3 i n t i d ;4 Record ( ) { i d = count ++; }5 s t a t i c vo id show_int ( i n t num ) {6 System . out . p r i n t l n (num ) ;7 }8 s t a t i c vo id show_count ( ) {9 show_int ( count ) ;

10 }11 vo id show_id ( ) {12 show_int ( i d ) ;13 }14 vo id show_id_and_count ( ) {15 show_id ( ) ;16 show_count ( ) ;17 }18 }

instance methods: can access all members/methodsstatic methods: can access static members/methodsH.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 27 / 55

Instance versus Class

Instance versus Class (2/2)

1 / / t h i s : means my2 c lass Record {3 s t a t i c i n t count ; i n t i d ;4 Record ( ) { t h i s . i d = Record . count ++; }5 s t a t i c vo id show_int ( i n t num ) {6 java . lang . System . out . p r i n t l n (num ) ;7 }8 s t a t i c vo id show_count ( ) {9 Record . show_int ( Record . count ) ;

10 }11 vo id show_id ( ) {12 Record . show_int ( t h i s . i d ) ;13 }14 vo id show_id_and_count ( ) {15 t h i s . show_id ( ) ; Record . show_count ( ) ;16 }17 }

what compiler sees: a fully qualified intentionthis: the “object” reference (known in run-time)Record: the “class” name (known in compile-time)H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 28 / 55

Instance versus Class

Instance versus Class: Key Point

null/non-null instance access classmethod/variable: as if YESclass access instance method/variable method:NOinstance method access class method/variable:YESclass method access instance method/variable:NO

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 29 / 55

Instance versus Class

this (1/3)

1 c lass Record {2 s t a t i c i n t count ; i n t i d ;3 Record ( ) { t h i s . i d = Record . count ++; }4 s t a t i c vo id show_int ( i n t num ) {5 java . lang . System . out . p r i n t l n (num ) ;6 }7 s t a t i c vo id show_id ( Record r ) {8 Record . show_int ( r . i d ) ;9 }

10 vo id or ig_show_id ( ) {11 Record . show_int ( t h i s . i d ) ;12 }13 }14 p u b l i c c lass RecordDemo {15 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {16 Record r = new Record ( ) ;17 r . or ig_show_id ( ) ; Record . show_id ( r ) ;18 }19 }

a static implementation of show_id“almost” what the compiler/JVM doesH.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 30 / 55

Instance versus Class

this (2/3)

1 c lass Record {2 s t a t i c i n t count ; i n t i d ;3 Record ( ) { t h i s . i d = Record . count ++; }4 s t a t i c vo id show_int ( i n t num ) {5 java . lang . System . out . p r i n t l n (num ) ;6 }7 s t a t i c vo id show_id ( Record THIS ) {8 Record . show_int ( THIS . i d ) ;9 }

10 vo id or ig_show_id ( ) {11 Record . show_int ( t h i s . i d ) ;12 }13 }14 p u b l i c c lass RecordDemo {15 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {16 Record r = new Record ( ) ;17 r . or ig_show_id ( ) ; Record . show_id ( r ) ;18 }19 }

implicitly, instance method can access an additionalreference-type parameter this passed from the callerH.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 31 / 55

Instance versus Class

this (3/3)

1 c lass Record {2 i n t score ;3 s t a t i c vo id show_score ;4 vo id se t_ to ( i n t score ) { t h i s . score = score ; }5 }

this: the reference to specifically say “my” variable/method

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 32 / 55

Instance versus Class

this: Key Point

this: the reference used to specifically say “mine”—implicit in every instance method

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 33 / 55

Recursive Calls

Recursive Calls (1/5)

1 c lass D i r { F i l e [ ] f i l e s ; D i r [ ] d i r s ; }2 c lass F i l e { S t r i n g name ; }3 p u b l i c c lass DirDemo {4 s t a t i c vo id l i s t A l l ( D i r cu r ren t ) {5 i n t i , j ;6 f o r ( i =0; i < f i l e s . leng th ; i ++)7 System . out . p r i n t l n ( f i l e s [ i ] . name ) ;8 f o r ( j =0; j < d i r s . leng th ; j ++)9 l i s t A l l ( d i r s [ j ] ) ;

10 }11 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {12 / / . . .13 Di r d = new Di r ( ) ;14 l i s t A l l ( d ) ;15 }16 }

recursive: when a method calls itself

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 34 / 55

Recursive Calls

Recursive Calls (2/5)

1 p u b l i c c lass FibDemo {2 s t a t i c i n t f i b ( i n t n ) {3 i n t res ;4 System . out . p r i n t l n ( " f i b ( " + n + " ) c a l l e d " ) ;5 i f ( n <= 1)6 res = 1;7 else8 res = f i b ( n−1) + f i b ( n−2);9 System . out . p r i n t l n ( " f i b ( " + n + " ) r e t u r n i n g " ) ;

10 r e t u r n res ;11 }12 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {13 System . out . p r i n t l n ( f i b ( 5 ) ) ;14 }15 }

method call: do last task first (recursive or non-recursive)method call stack: implement “last task first” (last-in-first-out)

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 35 / 55

Recursive Calls

Recursive Calls (3/5)

1 p u b l i c c lass FibDemo {2 s t a t i c i n t f i b ( i n t n ) {3 i n t res ;4 System . out . p r i n t l n ( " f i b ( " + n + " ) c a l l e d " ) ;5 i f ( n <= 1)6 res = 1;7 else {8 i n t m = n−1; res = f i b (m) ; res = res + f i b ( n−2);9 }

10 System . out . p r i n t l n ( " f i b ( " + n + " ) r e t u r n i n g " ) ;11 r e t u r n res ;12 }13 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) { i n t a = f i b ( 5 ) ; }14 }

what needs to be stored in each “frame”?my local version of nmy local version of resany temporary value generated by compiler (e.g. like m)(my return value and where I am returning to)

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 36 / 55

Recursive Calls

Recursive Calls (4/5)

1 p u b l i c c lass Fib {2 i n t N;3 Fib ( i n t N) { t h i s .N = N; }4 i n t get ( ) {5 i f (N <= 1)6 r e t u r n 1 ;7 else8 r e t u r n (new Fib (N−1)) . get ( ) + (new Fib (N−2)) . get ( ) ;9 }

10 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {11 Fib f = new Fib ( 2 0 ) ;12 System . out . p r i n t l n ( " res = " + f . get ( 5 ) ) ;13 System . out . p r i n t l n ( " res = " + f . get (10) ) ;14 }15 }

one advanced use: one class, two behaviorsutility behavior: static mainobject behavior: with a state

is get recursive? seems No (different objects) but actually Yes(same method with different “this”)H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 37 / 55

htlin
鉛筆

Recursive Calls

Recursive Calls (5/5)

1 p u b l i c c lass Fib {2 i n t MAXN; i n t [ ] computed ;3 Fib ( i n t MAXN) {4 t h i s .MAXN = MAXN; computed = new i n t [MAXN+1]5 }6 i n t get ( i n t n ) {7 i f ( computed [ n ] == 0 ) {8 i f ( n <= 1) computed [ n ] = 1 ;9 else computed [ n ] = get ( n−1) + get ( n−2);

10 }11 r e t u r n computed [ n ] ;12 }13 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {14 Fib f = new Fib ( 2 0 ) ;15 System . out . p r i n t l n ( " res = " + f . get ( 5 ) ) ;16 System . out . p r i n t l n ( " res = " + f . get (10) ) ;17 }18 }

recursive, but no repeated computationno need to create so many objectsthink: any better implementations?H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 38 / 55

htlin
鉛筆

Recursive Calls

Recursive Calls: Key Point

method call: not just “goto”—comes with a frame of status passing, storing, ma-nipulating, and returning

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 39 / 55

Local Variables

Local Variables (1/7)

1 p u b l i c c lass FibDemo {2 i n t f i b ( i n t n ) {3 i n t res ;4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;5 System . out . p r i n t l n ( s ) ;6 i f ( n <= 1)7 res = 1;8 else {9 res = f i b ( n−1);

10 res = res + f i b ( n−2);11 }12 r e t u r n res ;13 }14 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {15 FibDemo f = new FibDemo ( ) ;16 System . out . p r i n t l n ( " res = " + f . f i b ( 5 ) ) ;17 }18 }

local primitive n: allocated, and assigned by argument⇒parameter

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 40 / 55

Local Variables

Local Variables (2/7)

1 p u b l i c c lass FibDemo {2 i n t f i b ( i n t n ) {3 i n t res ;4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;5 System . out . p r i n t l n ( s ) ;6 i f ( n <= 1)7 res = 1;8 else {9 res = f i b ( n−1);

10 res = res + f i b ( n−2);11 }12 r e t u r n res ;13 }14 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {15 FibDemo f = new FibDemo ( ) ;16 System . out . p r i n t l n ( " res = " + f . f i b ( 5 ) ) ;17 }18 }

local referece this: allocated, and assigned by argument (f)⇒parameter (this)

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 41 / 55

Local Variables

Local Variables (3/7)

1 p u b l i c c lass FibDemo {2 i n t f i b ( i n t n ) {3 i n t res ;4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;5 System . out . p r i n t l n ( s ) ;6 i f ( n <= 1)7 res = 1;8 else {9 res = f i b ( n−1);

10 res = res + f i b ( n−2);11 }12 r e t u r n res ;13 }14 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {15 FibDemo f = new FibDemo ( ) ;16 System . out . p r i n t l n ( " res = " + f . f i b ( 5 ) ) ;17 }18 }

local primitive res: allocated, not initialized assigned by ourselves

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 42 / 55

Local Variables

Local Variables (4/7)

1 p u b l i c c lass FibDemo {2 i n t f i b ( i n t n ) {3 i n t res ;4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;5 System . out . p r i n t l n ( s ) ;6 i f ( n <= 1)7 res = 1;8 else {9 res = f i b ( n−1);

10 res = res + f i b ( n−2);11 }12 r e t u r n res ;13 }14 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {15 FibDemo f = new FibDemo ( ) ;16 System . out . p r i n t l n ( " res = " + f . f i b ( 5 ) ) ;17 }18 }

local reference s: reference allocated, not initialized, point to avalid object by ourselves

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 43 / 55

Local Variables

Local Variables (5/7)

1 p u b l i c c lass FibDemo {2 i n t f i b ( i n t n ) {3 i n t res ;4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;5 System . out . p r i n t l n ( s ) ;6 i f ( n <= 1)7 res = 1;8 else {9 res = f i b ( n−1);

10 res = res + f i b ( n−2);11 }12 r e t u r n res ;13 }14 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {15 FibDemo f = new FibDemo ( ) ;16 System . out . p r i n t l n ( " res = " + f . f i b ( 5 ) ) ;17 }18 }

some other local variables generated by compiler: allocated, notinitialized, used internally

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 44 / 55

Local Variables

Local Variables (6/7)

1 p u b l i c c lass FibDemo {2 i n t f i b ( i n t n ) {3 i n t res ;4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;5 System . out . p r i n t l n ( s ) ;6 i f ( n <= 1) res = 1;7 else res = f i b ( n−1) + f i b ( n−2);8 r e t u r n res ;9 }

10 p u b l i c s t a t i c vo id main ( S t r i n g [ ] arg ) {11 FibDemo f = new FibDemo ( ) ;12 System . out . p r i n t l n ( " res = " + f . f i b ( 5 ) ) ;13 }14 }

when call returns: the result is “copied” to the previous framesomewherelocal primitive: simply discardedlocal reference: discarded (and then the “object” GC’ed some timeif no longer used)H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 45 / 55

Local Variables

Local Variables (7/7, courtesy of Prof. Chuen-Liang Chen)

localvariable

instancevariable

class (static)variable

b l t method i t lbelong to methodinvocation instance class

declaration within method within class within classdifi i O O Smodifier static NO NO YES

allocation (when) methodinvocation

instancecreation

classloadingg

allocation (where) stack memory heap memory heap memoryinitial to 0 NO YES YES

de-allocation methodreturn

automaticgarbage

collectionNO

scope

usage range direct access rangefrom

declaration to whole class whole class

Chuen-Liang Chen, NTU CS&IE / 5OOP

declaration to end of block

whole class whole class

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 46 / 55

Local Variables

Local Variables: Key Point

local variables: the “status” of the current frame—by spec not necessarily initialized

H.-T. Lin (NTU CSIE) Be Concrete about Abstractions OOP(even) 03/24/2009 47 / 55

top related