Basic Java OOP
10/19/2015
Hsuan-Tien Lin (林軒田) htlin@csie.ntu.edu.tw
Department of Computer Science
& Information Engineering
National Taiwan University ( 國立台灣大學資訊工程系)
Hsuan-Tien Lin (NTU CSIE) Basic Java OOP 0/45
What We Have Done
•
designing object (what variables? what methods?)—[0] extended types, [1] method declaration/implementation
•
creating “first” object and calling its first action (done: java ClassName will call main)•
creating other objects —[2] new and beyond•
calling other objects (partially done: method invocation, will talk more in [1])•
manipulating object status (partially done: instance variable assignments)•
deleting objects —[TODO 3] object lifecycleMore on Constructors: Key Point
often better to use self-defined and overloaded constructors to help initialize
Hsuan-Tien Lin (NTU CSIE) 2/45
[3] Object Lifecycle
[3] Object Lifecycle
[3] Object Lifecycle
Garbage Collection (1/2)
1 p u b l i c c l a s s Record {
2 p r i v a t e i n t sc or e ;
3 }
4 p u b l i c c l a s s RecordDemo {
5 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
6 i n t i ; Record r 1 ;
7 f o r( i = 0 ; i < 100; i + + ) {
8 r 1 = new Record ( ) ;
9 }
10 }
11 }
•
100 instances created, only 1 alive after the loop•
the other 99 memory slots: automatically recycledHsuan-Tien Lin (NTU CSIE) 4/45
[3] Object Lifecycle
Garbage Collection (1/2)
1 p u b l i c c l a s s Record {
2 p r i v a t e i n t sc or e ;
3 }
4 p u b l i c c l a s s RecordDemo {
5 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
6 i n t i ; Record r 1 ;
7 f o r( i = 0 ; i < 100; i + + ) {
8 r 1 = new Record ( ) ;
9 }
10 }
11 }
•
100 instances created, only 1 alive after the loop•
the other 99 memory slots: automatically recycled[3] Object Lifecycle
Garbage Collection (2/2)
1 c l a s s Record {
2 p r i v a t e Record prev ;
3 p u b l i c Record ( Record p ) { prev = p ; }
4 }
5 p u b l i c c l a s s RecordDemo {
6 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
7 i n t i ; Record r 1 = n u l l;
8 f o r( i = 0 ; i < 100; i + + ) {
9 Record tmp = new Record ( r 1 ) ;
10 r 1 = tmp ;
11 }
12 }
13 }
•
100 instances created, all of them aliveHsuan-Tien Lin (NTU CSIE) 5/45
[3] Object Lifecycle
Garbage Collection (2/2)
1 c l a s s Record {
2 p r i v a t e Record prev ;
3 p u b l i c Record ( Record p ) { prev = p ; }
4 }
5 p u b l i c c l a s s RecordDemo {
6 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
7 i n t i ; Record r 1 = n u l l;
8 f o r( i = 0 ; i < 100; i + + ) {
9 Record tmp = new Record ( r 1 ) ;
10 r 1 = tmp ;
11 }
12 }
13 }
•
100 instances created, all of them alive[3] Object Lifecycle
Garbage Collection: Key Point
Garbage Collection: when a memory slot be- comes an orphan (and) system in need of memory
Hsuan-Tien Lin (NTU CSIE) 6/45
[3] Object Lifecycle
Finalizer (1/2)
1 p u b l i c c l a s s Record {
2 p r i v a t e i n t sc or e ;
3 p u b l i c Record ( ) { sys . mem_usage += 1 0 ; }
4 p u b l i c v o i d when_truck_comes ( ) { sys . mem_usage −= 1 0 ; }
5 }
6 p u b l i c c l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 i n t i ; Record r 1 ;
9 f o r( i = 0 ; i < 100; i + + ) {
10 r 1 = new Record ( ) ;
11 }
12 }
13 }
•
finalizer: something you want to do when truck comes•
calculate memory usage, write something back (say, on BBS), ...[3] Object Lifecycle
Finalizer (1/2)
1 p u b l i c c l a s s Record {
2 p r i v a t e i n t sc or e ;
3 p u b l i c Record ( ) { sys . mem_usage += 1 0 ; }
4 p u b l i c v o i d when_truck_comes ( ) { sys . mem_usage −= 1 0 ; }
5 }
6 p u b l i c c l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 i n t i ; Record r 1 ;
9 f o r( i = 0 ; i < 100; i + + ) {
10 r 1 = new Record ( ) ;
11 }
12 }
13 }
•
finalizer: something you want to do when truck comes•
calculate memory usage, write something back (say, on BBS), ...Hsuan-Tien Lin (NTU CSIE) 7/45
[3] Object Lifecycle
Finalizer (2/2)
1 p u b l i c c l a s s Record {
2 p r i v a t e i n t sc or e ;
3 p u b l i c Record ( ) { sys . mem_usage += 1 0 ; }
4 p r o t e c t e d v o i d f i n a l i z e ( ) t h r o w s Throwable {
5 sys . mem_usage −= 1 0 ;
6 System . o u t . p r i n t l n ( " Good Bye ! " ) ;
7 }
8 }
•
GC: no guarantee on when the truck comes•
if JVM halts before truck comes, even no finalizer calls• use carefully
[3] Object Lifecycle
Finalizer (2/2)
1 p u b l i c c l a s s Record {
2 p r i v a t e i n t sc or e ;
3 p u b l i c Record ( ) { sys . mem_usage += 1 0 ; }
4 p r o t e c t e d v o i d f i n a l i z e ( ) t h r o w s Throwable {
5 sys . mem_usage −= 1 0 ;
6 System . o u t . p r i n t l n ( " Good Bye ! " ) ;
7 }
8 }
•
GC: no guarantee on when the truck comes•
if JVM halts before truck comes, even no finalizer calls• use carefully
Hsuan-Tien Lin (NTU CSIE) 8/45
[3] Object Lifecycle
Finalizer: Key Point
finalizer:
a mechanism to let the instance say goodbye
[3] Object Lifecycle
Object Lifecycle (1/1)
1 p u b l i c c l a s s Record {
2 p r i v a t e i n t sc or e ;
3 p u b l i c Record (i n t i n i t _ s c o r e ) { sc or e = i n i t _ s c o r e ; }
4 p r o t e c t e d v o i d f i n a l i z e ( ) t h r o w s Throwable { }
5 }
6 p u b l i c c l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 Record r ; / / v a r i a b l e d e c l a r e d
9 r = new Record ( 6 0 ) ; / / memory a l l o c a t e d (RHS)
10 / / and c o n s t r u c t o r c a l l e d
11 / / v a r i a b l e l i n k e d ( LHS )
12 r . show_score ( ) ; / / i n s t a n c e a c t i o n performed
13 r = n u l l;/ / memory s l o t orphaned
14 / / . . . .
15 / / f i n a l i z e r c a l l e d
16 / / o r JVM t e r m i n a t e d
17 }
18 }
Hsuan-Tien Lin (NTU CSIE) 10/45
[3] Object Lifecycle
Object Lifecycle: Key Point
we control birth, life, death, funeral design, but not the exact funeral time
[4] Back to Class
[4] Back to Class
Hsuan-Tien Lin (NTU CSIE) 12/45
[4] Back to Class
Static Variables (1/3)
1 p u b l i c c l a s s Record {
2 p r i v a t e i n t t o t a l _ r e c ;
3 p u b l i c Record ( ) {
4 t o t a l _ r e c += 1 ;
5 }
6 p u b l i c v o i d s h o w _ t o t a l _ r e c ( ) {
7 System . o u t . p r i n t l n ( t o t a l _ r e c ) ;
8 }
9 }
10 p u b l i c c l a s s RecordDemo {
11 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
12 Record r 1 = new Record ( ) ;
13 r 1 . s h o w _ t o t a l _ r e c ( ) ;
14 Record r 2 = new Record ( ) ;
15 r 2 . s h o w _ t o t a l _ r e c ( ) ;
16 }
17 }
•
no shared space to store the total records[4] Back to Class
Static Variables (1/3)
1 p u b l i c c l a s s Record {
2 p r i v a t e i n t t o t a l _ r e c ;
3 p u b l i c Record ( ) {
4 t o t a l _ r e c += 1 ;
5 }
6 p u b l i c v o i d s h o w _ t o t a l _ r e c ( ) {
7 System . o u t . p r i n t l n ( t o t a l _ r e c ) ;
8 }
9 }
10 p u b l i c c l a s s RecordDemo {
11 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
12 Record r 1 = new Record ( ) ;
13 r 1 . s h o w _ t o t a l _ r e c ( ) ;
14 Record r 2 = new Record ( ) ;
15 r 2 . s h o w _ t o t a l _ r e c ( ) ;
16 }
17 }
•
no shared space to store the total recordsHsuan-Tien Lin (NTU CSIE) 13/45
[4] Back to Class
Static Variables (2/3)
1 p u b l i c c l a s s RecordShared {
2 p r i v a t e i n t cou nt ;
3 p u b l i c v o i d i n c r e a s e _ c o u n t ( ) { c oun t ++; }
4 p u b l i c i n t g e t _ c o u n t ( ) { r e t u r n c oun t ; }
5 }
6 c l a s s Record {
7 RecordShared shared ;
8 p u b l i c Record ( RecordShared s ) {
9 share = s ; shared . i n c r e a s e _ c o u n t ( ) ;
10 }
11 p u b l i c v o i d s h o w _ t o t a l _ r e c ( ) {
12 System . o u t . p r i n t l n ( shared . g e t _ c o u n t ( ) ) ;
13 }
14 }
15 p u b l i c c l a s s RecordDemo {
16 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
17 RecordShared shared_space = new RecordShared ( ) ;
18 Record r 1 = new Record ( shared_space ) ;
19 r 1 . s h o w _ t o t a l _ r e c ( ) ;
20 Record r 2 = new Record ( shared_space ) ;
21 r 2 . s h o w _ t o t a l _ r e c ( ) ;
22 }
23 }
•
do-able, but complicated, and requires many explicit steps[4] Back to Class
Static Variables (2/3)
1 p u b l i c c l a s s RecordShared {
2 p r i v a t e i n t cou nt ;
3 p u b l i c v o i d i n c r e a s e _ c o u n t ( ) { c oun t ++; }
4 p u b l i c i n t g e t _ c o u n t ( ) { r e t u r n c oun t ; }
5 }
6 c l a s s Record {
7 RecordShared shared ;
8 p u b l i c Record ( RecordShared s ) {
9 share = s ; shared . i n c r e a s e _ c o u n t ( ) ;
10 }
11 p u b l i c v o i d s h o w _ t o t a l _ r e c ( ) {
12 System . o u t . p r i n t l n ( shared . g e t _ c o u n t ( ) ) ;
13 }
14 }
15 p u b l i c c l a s s RecordDemo {
16 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
17 RecordShared shared_space = new RecordShared ( ) ;
18 Record r 1 = new Record ( shared_space ) ;
19 r 1 . s h o w _ t o t a l _ r e c ( ) ;
20 Record r 2 = new Record ( shared_space ) ;
21 r 2 . s h o w _ t o t a l _ r e c ( ) ;
22 }
23 }
•
do-able, but complicated, and requires many explicit stepsHsuan-Tien Lin (NTU CSIE) 14/45
[4] Back to Class
Static Variables (3/3)
1 p u b l i c c l a s s Record {
2 p r i v a t e s t a t i c i n t t o t a l _ r e c = 0 ;
3 p u b l i c Record ( ) { t o t a l _ r e c ++; }
4 p u b l i c v o i d s h o w _ t o t a l _ r e c ( ) {
5 System . o u t . 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 l a s s RecordDemo {
9 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
10 Record r 1 = new Record ( ) ;
11 r 1 . s h o w _ t o t a l _ r e c ( ) ;
12 Record r 2 = new Record ( ) ;
13 r 2 . s h o w _ t o t a l _ r e c ( ) ;
14 System . o u t . p r i n t l n ( Record . t o t a l _ r e c ) ;
15 }
16 }
•
static: shared between all X-type instances•
like a global variable within the scope of the class• use scarcely
[4] Back to Class
Static Variables (3/3)
1 p u b l i c c l a s s Record {
2 p r i v a t e s t a t i c i n t t o t a l _ r e c = 0 ;
3 p u b l i c Record ( ) { t o t a l _ r e c ++; }
4 p u b l i c v o i d s h o w _ t o t a l _ r e c ( ) {
5 System . o u t . 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 l a s s RecordDemo {
9 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
10 Record r 1 = new Record ( ) ;
11 r 1 . s h o w _ t o t a l _ r e c ( ) ;
12 Record r 2 = new Record ( ) ;
13 r 2 . s h o w _ t o t a l _ r e c ( ) ;
14 System . o u t . p r i n t l n ( Record . t o t a l _ r e c ) ;
15 }
16 }
•
static: shared between all X-type instances•
like a global variable within the scope of the class• use scarcely
Hsuan-Tien Lin (NTU CSIE) 15/45
[4] Back to Class
Static Variables: Key Point
staticvariable:
of the
class (shared), not of an instance
[4] Back to Class
Static Variables Revisited (1/1)
1 p u b l i c c l a s s Record {
2 p r i v a t e s t a t i c i n t t o t a l _ r e c = 0 ;
3 p r i v a t e 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 l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 Record r 1 = new Record ( ) ;
9 Record r 2 = n u l l;
10 Record r 3 = new Record ( ) ;
11 System . o u t . p r i n t l n ( r 1 . t o t a l _ r e c ) ;
12 System . o u t . p r i n t l n ( r 2 . t o t a l _ r e c ) ;
13 System . o u t . p r i n t l n ( Record . t o t a l _ r e c ) ;
14 System . o u t . p r i n t l n ( r 1 . i d ) ;
15 System . o u t . p r i n t l n ( r 2 . i d ) ;
16 System . o u t . p r i n t l n ( Record . i d ) ;
17 }
18 }
•
r2.total_rec⇒ Record.total_rec in compile timeHsuan-Tien Lin (NTU CSIE) 17/45
[4] Back to Class
Static Variables Revisited (1/1)
1 p u b l i c c l a s s Record {
2 p r i v a t e s t a t i c i n t t o t a l _ r e c = 0 ;
3 p r i v a t e 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 l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 Record r 1 = new Record ( ) ;
9 Record r 2 = n u l l;
10 Record r 3 = new Record ( ) ;
11 System . o u t . p r i n t l n ( r 1 . t o t a l _ r e c ) ;
12 System . o u t . p r i n t l n ( r 2 . t o t a l _ r e c ) ;
13 System . o u t . p r i n t l n ( Record . t o t a l _ r e c ) ;
14 System . o u t . p r i n t l n ( r 1 . i d ) ;
15 System . o u t . p r i n t l n ( r 2 . i d ) ;
16 System . o u t . p r i n t l n ( Record . i d ) ;
17 }
18 }
•
r2.total_rec⇒ Record.total_rec in compile time[4] Back to Class
Static Variables Revisited: Key Point
staticvariable:
of the
class (shared), not of an instance;
compile-time binding (i.e. static binding)
Hsuan-Tien Lin (NTU CSIE) 18/45
[4] Back to Class
Static Methods (1/2)
1 p u b l i c c l a s s myMath {
2 p u b l 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 l a s s MathDemo {
7 p u b l i c s t a t i c v o i d 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 . o u t . p r i n t l n (m. mean ( i , j ) ) ;
12 }
13 }
•
new a myMath instance just for computing mean –unnecessary[4] Back to Class
Static Methods (1/2)
1 p u b l i c c l a s s myMath {
2 p u b l 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 l a s s MathDemo {
7 p u b l i c s t a t i c v o i d 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 . o u t . p r i n t l n (m. mean ( i , j ) ) ;
12 }
13 }
•
new a myMath instance just for computing mean –unnecessaryHsuan-Tien Lin (NTU CSIE) 19/45
[4] Back to Class
Static Methods (2/2)
1 c l a s s 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 l a s s MathDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 double i = 3 . 5 ;
9 double j = 2 . 4 ;
10 System . o u t . p r i n t l n ( myMath . mean ( i , j ) ) ;
11 System . o u t . 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 instance
•
similar to static variable usage[4] Back to Class
Static Methods (2/2)
1 c l a s s 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 l a s s MathDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 double i = 3 . 5 ;
9 double j = 2 . 4 ;
10 System . o u t . p r i n t l n ( myMath . mean ( i , j ) ) ;
11 System . o u t . 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 instance
•
similar to static variable usageHsuan-Tien Lin (NTU CSIE) 20/45
[4] Back to Class
Static Methods: Key Point
staticmethod:
associated with the
class,
no need to create an instance[4] Back to Class
Use of Static Methods (1/2)
1 p u b l i c c l a s s UtilDemo {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
3 System . o u t . p r i n t l n ( Math . PI ) ;
4 System . o u t . p r i n t l n ( Math . s q r t ( 2 . 0 ) ) ;
5 System . o u t . p r i n t l n ( Math . max ( 3 . 0 , 5 . 0 ) ) ;
6 System . o u t . p r i n t l n ( I n t e g e 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 instance)•
main is static (called by classname during ’java className’)•
tools for other static methodsHsuan-Tien Lin (NTU CSIE) 22/45
[4] Back to Class
Use of Static Methods (1/2)
1 p u b l i c c l a s s UtilDemo {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
3 System . o u t . p r i n t l n ( Math . PI ) ;
4 System . o u t . p r i n t l n ( Math . s q r t ( 2 . 0 ) ) ;
5 System . o u t . p r i n t l n ( Math . max ( 3 . 0 , 5 . 0 ) ) ;
6 System . o u t . p r i n t l n ( I n t e g e 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 instance)•
main is static (called by classname during ’java className’)•
tools for other static methods[4] Back to Class
Use of Static Methods (2/2)
1 c l a s s Record {
2 p r i v a t e s t a t i c i n t t o t a l _ r e c = 0 ;
3 p u b l i c Record ( ) { t o t a l _ r e c ++; }
4 p u b l i c s t a t i c v o i d s h o w _ t o t a l _ r e c ( ) {
5 System . o u t . 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 l a s s RecordDemo {
9 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
10 Record r 1 = new Record ( ) ;
11 Record . s h o w _ t o t a l _ r e c ( ) ;
12 }
13 }
•
class related actions rather than instance related actionsHsuan-Tien Lin (NTU CSIE) 23/45
[4] Back to Class
Use of Static Methods (2/2)
1 c l a s s Record {
2 p r i v a t e s t a t i c i n t t o t a l _ r e c = 0 ;
3 p u b l i c Record ( ) { t o t a l _ r e c ++; }
4 p u b l i c s t a t i c v o i d s h o w _ t o t a l _ r e c ( ) {
5 System . o u t . 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 l a s s RecordDemo {
9 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
10 Record r 1 = new Record ( ) ;
11 Record . s h o w _ t o t a l _ r e c ( ) ;
12 }
13 }
•
class related actions rather than instance related actions[4] Back to Class
Use of Static Methods: Key Point
staticmethod:
•
compile time determined (bound)•
per class•
sometimes usefulHsuan-Tien Lin (NTU CSIE) 24/45
[4] Back to Class
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 ;
[4] Back to Class
Life Cycle of a Primitive Variable (C/Java)
•
declared and created1 i n t c oun t ;
•
used and modified1 co unt += 1 ;
•
destroyed–automatically (when out of scope)
Hsuan-Tien Lin (NTU CSIE) 26/45
[4] Back to Class
Life Cycle of a Primitive Variable (C/Java)
•
declared and created1 i n t c oun t ;
•
used and modified1 co unt += 1 ;
•
destroyed–automatically (when out of scope)
[4] Back to Class
Life Cycle of a Primitive Variable (C/Java)
•
declared and created1 i n t c oun t ;
•
used and modified1 co unt += 1 ;
•
destroyed–automatically (when out of scope)
Hsuan-Tien Lin (NTU CSIE) 26/45
[4] Back to Class
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 ;
[4] Back to Class
Fun Time (3)
What happens in memory?
1 Record r 1 ; / / r 1 . name , r 1 . sc or e
2 Record r 2 ;
3 r 1 = new Record ( ) ;
4 r 2 = r 1 ; / / how many r e c o r d s are t h e r e ?
5 r 1 . name = " HTLin " ;
6 r 2 . sc or e = 9 8 ;
Hsuan-Tien Lin (NTU CSIE) 28/45
[4] Back to Class
Fun Time (4)
What happens in memory?
1 c l a s s 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 ;
[4] Back to Class
Fun Time (5)
What happens in memory?
1 c l a s s 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 = " L i s a " ;
Hsuan-Tien Lin (NTU CSIE) 30/45
[4] Back to Class
Life Cycle of an Object Instance (Java)
•
reference declared1 Record r ;
•
instance created1 r = new Record ( ) ;
•
used and modified1 System . o u t . p r i n t l n ( r . name ) ;
•
destroyed–automatically (when out of
use)
[4] Back to Class
Life Cycle of an Object Instance (Java)
•
reference declared1 Record r ;
•
instance created1 r = new Record ( ) ;
•
used and modified1 System . o u t . p r i n t l n ( r . name ) ;
•
destroyed–automatically (when out of
use)
Hsuan-Tien Lin (NTU CSIE) 31/45
[4] Back to Class
Life Cycle of an Object Instance (Java)
•
reference declared1 Record r ;
•
instance created1 r = new Record ( ) ;
•
used and modified1 System . o u t . p r i n t l n ( r . name ) ;
•
destroyed–automatically (when out of
use)
[4] Back to Class
Life Cycle of an Object Instance (Java)
•
reference declared1 Record r ;
•
instance created1 r = new Record ( ) ;
•
used and modified1 System . o u t . p r i n t l n ( r . name ) ;
•
destroyed–automatically (when out of
use)
Hsuan-Tien Lin (NTU CSIE) 31/45
[4] Back to Class
null Revisited (1/2)
1 c l a s s Record {
2 S t r i n g name ;
3 S t r i n g ID ;
4 i n t sc or e ;
5 }
6
7 p u b l i c c l a s s RecordDemo {
8 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
9 Record r 1 = new Record ( ) ;
10 System . o u t . p r i n t l n ( r 1 . sc or e ) ;
11 System . o u t . p r i n t l n ( r 1 . 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”[4] Back to Class
null Revisited (1/2)
1 c l a s s Record {
2 S t r i n g name ;
3 S t r i n g ID ;
4 i n t sc or e ;
5 }
6
7 p u b l i c c l a s s RecordDemo {
8 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
9 Record r 1 = new Record ( ) ;
10 System . o u t . p r i n t l n ( r 1 . sc or e ) ;
11 System . o u t . p r i n t l n ( r 1 . 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”Hsuan-Tien Lin (NTU CSIE) 32/45
[4] Back to Class
null Revisited (2/2)
1 c l a s s Record {
2 S t r i n g name ;
3 S t r i n g ID ;
4 i n t sc or e ;
5 }
6
7 p u b l i c c l a s s RecordDemo {
8 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
9 Record r 1 = n u l l;
10 System . o u t . p r i n t l n ( r 1 . sc or e ) ;
11 System . o u t . p r i n t l n ( r 1 . name ) ;
12 }
13 }
•
null pointer exception (run time error): accessing the component of “no reference”[4] Back to Class
null Revisited (2/2)
1 c l a s s Record {
2 S t r i n g name ;
3 S t r i n g ID ;
4 i n t sc or e ;
5 }
6
7 p u b l i c c l a s s RecordDemo {
8 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
9 Record r 1 = n u l l;
10 System . o u t . p r i n t l n ( r 1 . sc or e ) ;
11 System . o u t . p r i n t l n ( r 1 . name ) ;
12 }
13 }
•
null pointer exception (run time error): accessing the component of “no reference”Hsuan-Tien Lin (NTU CSIE) 33/45
[4] Back to Class
null Revisited: Key Point
null: Java’s special way of saying “no refer- ence”
[4] Back to Class
Reference Equal (1/2)
1 c l a s s Record {
2 S t r i n g name ;
3 i n t sc or e ;
4 }
5
6 p u b l i c c l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 Record r1 , r 2 ;
9 r 1 = new Record ( ) ; r 2 = new Record ( ) ;
10 r 1 . name = " HTLin " ; r 1 . s co re = 9 5 ;
11 r 2 . name = " HTLin " ; r 2 . s co re = 9 5 ;
12 System . o u t . p r i n t l n ( r 1 == r 2 ) ;
13 r 2 = r 1 ;
14 System . o u t . p r i n t l n ( r 1 == r 2 ) ;
15 }
16 }
•
reference equal: comparison by “reference value”Hsuan-Tien Lin (NTU CSIE) 35/45
[4] Back to Class
Reference Equal (1/2)
1 c l a s s Record {
2 S t r i n g name ;
3 i n t sc or e ;
4 }
5
6 p u b l i c c l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 Record r1 , r 2 ;
9 r 1 = new Record ( ) ; r 2 = new Record ( ) ;
10 r 1 . name = " HTLin " ; r 1 . s co re = 9 5 ;
11 r 2 . name = " HTLin " ; r 2 . s co re = 9 5 ;
12 System . o u t . p r i n t l n ( r 1 == r 2 ) ;
13 r 2 = r 1 ;
14 System . o u t . p r i n t l n ( r 1 == r 2 ) ;
15 }
16 }
•
reference equal: comparison by “reference value”[4] Back to Class
Reference Equal (2/2)
1 c l a s s Record {
2 S t r i n g name ;
3 i n t sc or e ;
4 }
5
6 p u b l i c c l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 Record r1 , r 2 ;
9 r 1 = n u l l; r 2 = new Record ( ) ;
10 System . o u t . p r i n t l n ( r 1 == r 2 ) ;
11 r 2 = r 1 ;
12 System . o u t . p r i n t l n ( r 1 == r 2 ) ;
13 }
14 }
•
nulldoes not equal non-null ... o_O•
nullequals null ... O_oHsuan-Tien Lin (NTU CSIE) 36/45
[4] Back to Class
Reference Equal (2/2)
1 c l a s s Record {
2 S t r i n g name ;
3 i n t sc or e ;
4 }
5
6 p u b l i c c l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 Record r1 , r 2 ;
9 r 1 = n u l l; r 2 = new Record ( ) ;
10 System . o u t . p r i n t l n ( r 1 == r 2 ) ;
11 r 2 = r 1 ;
12 System . o u t . p r i n t l n ( r 1 == r 2 ) ;
13 }
14 }
•
nulldoes not equal non-null ... o_O•
nullequals null ... O_o[4] Back to Class
Reference Equal: Key Point
==: reference equal rather than content equal for extended types
Hsuan-Tien Lin (NTU CSIE) 37/45
[4] Back to Class
String Equal (1/1)
1 p u b l i c c l a s s 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 v o i d main ( S t r i n g [ ] arg ) {
5 s1 = " HTLin " ;
6 s2 = " HTLin " ;
7 System . o u t . 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 . o u t . p r i n t l n ( s1 == s2 ) ;
11 System . o u t . p r i n t l n ( s1 . e q u a l s ( 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[4] Back to Class
String Equal (1/1)
1 p u b l i c c l a s s 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 v o i d main ( S t r i n g [ ] arg ) {
5 s1 = " HTLin " ;
6 s2 = " HTLin " ;
7 System . o u t . 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 . o u t . p r i n t l n ( s1 == s2 ) ;
11 System . o u t . p r i n t l n ( s1 . e q u a l s ( s2 ) ) ;
12 }
13 }
•
first true: compiler allocates one constant string only•
second false: two different string references•
third true: an action (method) for content comparisonHsuan-Tien Lin (NTU CSIE) 38/45
[4] Back to Class
String Equal: Key Point
String ==: still reference equal, use .equals if want content equal
[4] Back to Class
Reference Argument/Parameter (1/3)
1 c l a s s Tool {
2 b o o l 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 l a s s Demo{
8 p u b l i c s t a t i c v o i d 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 . o u t . p r i n t l n ( t . t r i c k y ( sa , sb ) ) ;
13 System . o u t . p r i n t l n ( sa == sb ) ;
14 System . o u t . 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’tHsuan-Tien Lin (NTU CSIE) 40/45
[4] Back to Class
Reference Argument/Parameter (1/3)
1 c l a s s Tool {
2 b o o l 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 l a s s Demo{
8 p u b l i c s t a t i c v o i d 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 . o u t . p r i n t l n ( t . t r i c k y ( sa , sb ) ) ;
13 System . o u t . p r i n t l n ( sa == sb ) ;
14 System . o u t . 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[4] Back to Class
Reference Argument/Parameter (2/3)
1 c l a s s m yI nt {i n t v a l ; m yI nt (i n t v ) { v a l = v ; } }
2 c l a s s Tool {
3 v o i d swap ( m yI nt f i r s t , m yI nt 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 . o u t . p r i n t l n ( f i r s t . v a l ) ;
8 System . o u t . p r i n t l n ( second . v a l ) ;
9 }
10 }
11 p u b l i c c l a s s Demo{
12 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
13 Tool t = new Tool ( ) ;
14 m yI nt i = new m yI nt ( 3 ) ;
15 m yI nt j = new m yI nt ( 5 ) ;
16 t . swap ( i , j ) ;
17 System . o u t . p r i n t l n ( i . v a l ) ;
18 System . o u t . p r i n t l n ( j . v a l ) ;
19 }
20 }
•
swapped as requestedHsuan-Tien Lin (NTU CSIE) 41/45
[4] Back to Class
Reference Argument/Parameter (2/3)
1 c l a s s m yI nt {i n t v a l ; m yI nt (i n t v ) { v a l = v ; } }
2 c l a s s Tool {
3 v o i d swap ( m yI nt f i r s t , m yI nt 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 . o u t . p r i n t l n ( f i r s t . v a l ) ;
8 System . o u t . p r i n t l n ( second . v a l ) ;
9 }
10 }
11 p u b l i c c l a s s Demo{
12 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
13 Tool t = new Tool ( ) ;
14 m yI nt i = new m yI nt ( 3 ) ;
15 m yI nt j = new m yI nt ( 5 ) ;
16 t . swap ( i , j ) ;
17 System . o u t . p r i n t l n ( i . v a l ) ;
18 System . o u t . p r i n t l n ( j . v a l ) ;
19 }
20 }
•
swapped as requested[4] Back to Class
Reference Argument/Parameter (3/3)
1 c l a s s m yI nt {i n t v a l ; m yI nt (i n t v ) { v a l = v ; } }
2 c l a s s Tool {
3 v o i d swap ( m yI nt f i r s t , m yI nt second ) {
4 m yI nt tmp = f i r s t ;
5 f i r s t = second ;
6 second = tmp ;
7 System . o u t . p r i n t l n ( f i r s t . v a l ) ;
8 System . o u t . p r i n t l n ( second . v a l ) ;
9 }
10 }
11 p u b l i c c l a s s Demo{
12 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
13 Tool t = new Tool ( ) ;
14 m yI nt i = new m yI nt ( 3 ) ;
15 m yI nt j = new m yI nt ( 5 ) ;
16 t . swap ( i , j ) ;
17 System . o u t . p r i n t l n ( i . v a l ) ;
18 System . o u t . p r i n t l n ( j . v a l ) ;
19 }
20 }
•
what happens?Hsuan-Tien Lin (NTU CSIE) 42/45
[4] Back to Class
Reference Argument/Parameter (3/3)
1 c l a s s m yI nt {i n t v a l ; m yI nt (i n t v ) { v a l = v ; } }
2 c l a s s Tool {
3 v o i d swap ( m yI nt f i r s t , m yI nt second ) {
4 m yI nt tmp = f i r s t ;
5 f i r s t = second ;
6 second = tmp ;
7 System . o u t . p r i n t l n ( f i r s t . v a l ) ;
8 System . o u t . p r i n t l n ( second . v a l ) ;
9 }
10 }
11 p u b l i c c l a s s Demo{
12 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
13 Tool t = new Tool ( ) ;
14 m yI nt i = new m yI nt ( 3 ) ;
15 m yI nt j = new m yI nt ( 5 ) ;
16 t . swap ( i , j ) ;
17 System . o u t . p r i n t l n ( i . v a l ) ;
18 System . o u t . p r i n t l n ( j . v a l ) ;
19 }
20 }
•
what happens?[4] Back to Class
Reference Argument/Parameter: Key Point
argument ⇒ parameter: by reference copying
same for return value
Hsuan-Tien Lin (NTU CSIE) 43/45
[4] Back to Class
this (1/1)
1 c l a s s Record {
2 i n t sc or e ;
3 v o i d s e t _ t o (i n t s co re ) { t h i s. sc or e = sc or e ; }
4 v o i d a d j u s t _ s c o r e { t h i s. s e t _ t o ( sc or e + 1 0 ) ; }
5 }
•
which score? which set_to?•
this: my (the object’s)[4] Back to Class
this (1/1)
1 c l a s s Record {
2 i n t sc or e ;
3 v o i d s e t _ t o (i n t s co re ) { t h i s. sc or e = sc or e ; }
4 v o i d a d j u s t _ s c o r e { t h i s. s e t _ t o ( sc or e + 1 0 ) ; }
5 }
•
which score? which set_to?•
this: my (the object’s)Hsuan-Tien Lin (NTU CSIE) 44/45
[4] Back to Class
this: Key Point
this: the reference variable pointing to the ob- ject itself