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) Referenc

Similar documents
3.1 num = 3 ch = 'C' 2

Chapter 9: Objects and Classes

EJB-Programming-4-cn.doc

EJB-Programming-3.PDF

Microsoft Word - 物件導向編程精要.doc

C++ 程式設計

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

Microsoft Word - ch04三校.doc

論 文 摘 要 本 文 乃 係 兩 岸 稅 務 爭 訟 制 度 之 研 究, 蓋 稅 務 爭 訟 在 行 訴 訟 中 一 直 占 有 相 當 高 的 比 例, 惟 其 勝 訴 率 一 直 偏 低, 民 87 年 10 月 28 日 行 訴 訟 法 經 幅 修 正 後, 審 級 部 分 由 一 級 一

untitled

從詩歌的鑒賞談生命價值的建構

Microsoft PowerPoint - CH 04 Techniques of Circuit Analysis

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

VASP应用运行优化

mvc

Strings

提纲 1 2 OS Examples for 3

Chapter 9: Objects and Classes

穨1-林聖欽.doc

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

Java

1-1 database columnrow record field 不 DBMS Access Paradox SQL Server Linux MySQL Oracle IBM Informix IBM DB2 Sybase 1-2

Microsoft Word - 第四組心得.doc

Microsoft Word - ED-774.docx

國立中山大學學位論文典藏.PDF

Open topic Bellman-Ford算法与负环

威 福 髮 藝 店 桃 園 市 蘆 竹 區 中 山 里 福 祿 一 街 48 號 地 下 一 樓 50,000 獨 資 李 依 純 105/04/06 府 經 登 字 第 號 宏 品 餐 飲 桃 園 市 桃 園 區 信 光 里 民

C 1

<4D F736F F D20ABCAADB1A142A5D8BFFDBB50A4A4AD5EBA4BAD6E2E646F63>

CC213

基于ECO的UML模型驱动的数据库应用开发1.doc

中國的科學與中國的公民:大陸研究在台灣的困境\\

INTRODUCTION TO COM.DOC

untitled

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

Microsoft Word - 論文封面 修.doc

致 謝 在 研 究 所 這 段 期 間 受 到 了 許 多 人 的 幫 助, 才 有 今 日 我 創 作 及 論 文 的 樣 貌 首 先 我 要 謝 謝 我 的 爸 媽, 知 道 我 自 小 就 喜 歡 塗 塗 畫 畫, 高 中 開 始 為 了 準 備 考 美 術 系 而 每 日 下 課 後 往 畫

中国的知识分子与民间(社会)

untitled

Microsoft PowerPoint - plan06.ppt

3. 圖 解 策 略 (1) 利 用 流 程 圖 心 智 圖 分 類 表 等 圖 表 整 理 重 點, 並 分 析 重 點 的 關 係 (2) 分 析 文 意 脈 絡 結 構 圖, 了 解 脈 絡 的 關 聯 性 三 閱 讀 策 略 的 使 用 建 議 : 1. 教 學 使 用 建 議 ( 詳 見

概述

实数集的程序数子集

83-112

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

Microsoft PowerPoint - ATF2015.ppt [相容模式]

高中英文科教師甄試心得

Microsoft Word - TIP006SCH Uni-edit Writing Tip - Presentperfecttenseandpasttenseinyourintroduction readytopublish

SB 綱 領 : (1) 消 防 服 務 管 制 人 員 : 就 年 度 需 要 特 別 留 意 的 事 項 中, 當 局 提 到 年 度 內, 消 防 處 會 啟 用 啟 德 新 建 並 設 有 救 護 設 施 的 消 防 局, 請 告 知 有 關

A dissertation for Master s degree Metro Indoor Coverage Systems Analysis And Design Author s Name: Sheng Hailiang speciality: Supervisor:Prof.Li Hui,

K7VT2_QIG_v3

<4D F736F F D C4EAC0EDB9A4C0E04142BCB6D4C4B6C1C5D0B6CFC0FDCCE2BEABD1A15F325F2E646F63>

59 1 CSpace 2 CSpace CSpace URL CSpace 1 CSpace URL 2 Lucene 3 ID 4 ID Web 1. 2 CSpace LireSolr 3 LireSolr 3 Web LireSolr ID

ΧΧΧΧ课程教学大纲(黑体,三号,段后1行)

從篤加有二「區」談當代平埔文化復振現相

國立中山大學學位論文典藏.PDF

20-25%,

IP505SM_manual_cn.doc

untitled

学校代号 学 号

南華大學數位論文

2006 3,,,,,, :, : ( [1996 ]1998 :396) : ( [1998 ]1999 :274), :,,,,,,,,,,,,,,,,, ([1962 ]1993 : ),,( ),,,, concordiadiscors ( ) 2, 2,,,, ( ),,,,


本 論 文 獲 客 家 委 員 會 102 年 客 家 研 究 優 良 博 碩 士 論 文 獎 助 謹 此 致 謝

FY.DOC

4. 每 组 学 生 将 写 有 习 语 和 含 义 的 两 组 卡 片 分 别 洗 牌, 将 顺 序 打 乱, 然 后 将 两 组 卡 片 反 面 朝 上 置 于 课 桌 上 5. 学 生 依 次 从 两 组 卡 片 中 各 抽 取 一 张, 展 示 给 小 组 成 员, 并 大 声 朗 读 卡

硕 士 学 位 论 文 论 文 题 目 : 北 岛 诗 歌 创 作 的 双 重 困 境 专 业 名 称 : 中 国 现 当 代 文 学 研 究 方 向 : 中 国 新 诗 研 究 论 文 作 者 : 奚 荣 荣 指 导 老 师 : 姜 玉 琴 2014 年 12 月

PowerPoint Presentation

Microsoft Word - 生活禮儀柯友惠981

第3章.doc

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

<453A5CB1BED0A3CBB6CABFC2DBCEC45C3037BCB6CBB6CABFC2DBCEC4A3A A3A95C BDECC8CBCEC4D1A7D4BAB1CFD2B5C2DBCEC45CC3F1CBD7D1A75CBAFAB1FEC4EAB1CFD2B5C2DBCEC4A3A8CEDED2B3C3BCA3A92E646F63>

(Microsoft Word - \251I\250D\245D\246W

Microsoft Word - Learn Objective-C.doc

恩 典 课 堂 教 学 概 览 课 堂 环 节 持 续 时 间 活 动 所 需 材 料 1 欢 迎 持 续 在 门 口 欢 迎 学 生, 聆 听 他 们 分 享 本 周 开 心 或 烦 恼 的 事 预 备 活 动 <10 分 钟 A 猜 猜 是 谁 B 上 帝 的 礼 物 无 孩 子 们 的 儿 时

团 契 就 体 力 来 说, 参 孙 乃 是 地 上 极 强 壮 的 人 ; 但 在 自 制 忠 贞 和 坚 稳 上, 他 却 是 人 间 最 软 弱 的 了 先 祖 与 先 知 第 页 教 室 布 置 见 第 一 课 课 堂 教 学 概 览 课 堂 环 节 持 续 时 间 活 动

錄...1 說...2 說 說...5 六 率 POST PAY PREPAY DEPOSIT 更

<4D F736F F D20D6D0CEC4B7A88C57B454CABF8C57CEBBD593CEC4D28EB9A0>

電機工程系認可證照清單 /7/1

天 主 教 輔 仁 大 學 社 會 學 系 學 士 論 文 小 別 勝 新 婚? 久 別 要 離 婚? 影 響 遠 距 家 庭 婚 姻 感 情 因 素 之 探 討 Separate marital relations are getting better or getting worse? -Exp


RunPC2_.doc

級 任 介 紹 班 主 任 其 他 級 任 2A 羅 彩 岫 主 任 吳 毅 蕙 老 師 2B 徐 玲 老 師 陳 詠 詩 老 師 2C 梁 麗 興 老 師 尹 南 老 師 2D 曾 康 老 師 李 樂 雯 老 師 2E 黃 蕙 明 老 師

Microsoft Word - GRE归来谈感想.doc

目 录 第 一 章 电 力 行 业 内 部 控 制 操 作 指 南 概 述... 1 第 二 章 内 部 控 制 规 范 体 系 建 设 与 运 行 第 三 章 内 部 环 境 建 设 第 一 节 组 织 架 构 第 二 节 发 展 战 略 第 三 节

3 Why would Chen risk ending the recent dance of détente between Taipei and Beijing a dance he has helped choreograph? Political analysts say Chen in


ebook140-9

<4D F736F F D20312E5FA473AEFCB867AED5AA605FBB50B04BCFC8AABAAFABB8DCACE3A8732E646F63>

Construction of Chinese pediatric standard database A Dissertation Submitted for the Master s Degree Candidate:linan Adviser:Prof. Han Xinmin Nanjing

: ( ),,,,, 1958,,, , 263, 231, ,,,,,,, 4, 51, 5, 46, 1950, :,, 839, 3711, ( ) ( ) 20 ( ),, 56, 2, 17, 2, 8, 1,,,,, :,,,, ;,,,,

谈屈原之死与其悲剧人格

218 台灣文學研究學報 第五期 一般論文 結構的當時 作為一個企圖和日語本位主義文化擴張進行抵抗 扭轉不均衡文化流向的批判性知識社群 以民間文學整理作為鏈 接的契機 以學院資源取得合法關鍵 不計採取民族文學遺產 化 知識化的柔軟姿態 開拓創作與言論空間 林荊南等人的努 力具有不可漠視的文化價值與啟

6張謇教育思想之研究.doc

C/C++ 语言 - 循环


encourages children to develop rich emotions through close contact with surrounding nature. It also cultivates a foundation for children s balanced de

Microsoft Word doc

Transcription:

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