references
play

References 7 January 2019 OSU CSE 1 Primitive vs. Reference Types - PowerPoint PPT Presentation

References 7 January 2019 OSU CSE 1 Primitive vs. Reference Types Java types are divided into two different categories: The built-in types are called primitive types Includes boolean , char , int , double All other types are


  1. References 7 January 2019 OSU CSE 1

  2. Primitive vs. Reference Types • Java types are divided into two different categories: – The built-in types are called primitive types • Includes boolean , char , int , double – All other types are called reference types (or class types ) • Includes String , XMLTree , SimpleReader , SimpleWriter , NaturalNumber , ... 7 January 2019 OSU CSE 2

  3. Primitive vs. Reference Types There is no limit on • Java types are divided into two different the number of other categories: user-defined types that can be – The built-in types are called primitive types developed. • Includes boolean , char , int , double – All other types are called reference types (or class types ) • Includes String , XMLTree , SimpleReader , SimpleWriter , NaturalNumber , ... 7 January 2019 OSU CSE 3

  4. Categories of Types, v. 1 Primitive Reference Types Types boolean String SimpleReader char XMLTree SimpleWriter int ... NaturalNumber double ... (plus 4 others) 7 January 2019 OSU CSE 4

  5. Primitive vs. Reference Variables • A primitive variable is a variable of a primitive type – This term is used sparingly in practice, and is introduced here for parsimony to distinguish a variable of a primitive type from… • A reference variable is a variable of a reference type – A reference variable is fundamentally different from a primitive variable in ways that can dramatically impact how you reason about program behavior; beware! 7 January 2019 OSU CSE 5

  6. Examples true 'y' 13 3.14 "Go" 7 January 2019 OSU CSE 6

  7. Examples true 'y' 13 3.14 "Go" 7 January 2019 OSU CSE 7

  8. Recall We Said Earlier... "Go" This is a String variable s whose value is "Go" , i.e., s = "Go" 7 January 2019 OSU CSE 8

  9. ... But Here’s the “Real Picture”! "Go" 7 January 2019 OSU CSE 9

  10. ... But Here’s the “Real Picture”! "Go" There is a String variable s , whose value is a reference to an object whose value is "Go" . 7 January 2019 OSU CSE 10

  11. References and Objects "Go" This is the reference s . 7 January 2019 OSU CSE 11

  12. References and Objects "Go" This is the object s “points to” or “refers to”. 7 January 2019 OSU CSE 12

  13. Reference and Object Values • A reference variable like s may be considered to have either of two values: – The reference value of s in these pictures is the memory address at which the object is stored – The object value of s in these pictures is the mathematical model value of the object the reference s points to, in this case "Go" 7 January 2019 OSU CSE 13

  14. Reference and Object Values Think of the reference value as simply an “id” or “serial number” of some place in memory. • A reference variable like s may be considered to have either of two values: – The reference value of s in these pictures is the memory address at which the object is stored – The object value of s in these pictures is the mathematical model value of the object the reference s points to, in this case "Go" 7 January 2019 OSU CSE 14

  15. Getting to the “Real Picture” Variables Objects … … b true 19 c 'y' 20 i 13 21 d 3.14 "Go" 22 s 22 23 24 … … 7 January 2019 OSU CSE 15

  16. Getting to the “Real Picture” Variables Objects … … b true 19 This part of memory holds: c 'y' 20 i 13 21 • values of primitive variables • reference values of reference d 3.14 "Go" 22 variables s 22 23 24 … … 7 January 2019 OSU CSE 16

  17. Getting to the “Real Picture” Variables Objects … … b true 19 c 'y' 20 This part of memory holds: i 13 21 • object values of reference variables d 3.14 "Go" 22 s 22 23 24 … … 7 January 2019 OSU CSE 17

  18. Getting to the “Real Picture” Variables Objects Each object in memory has a … … unique memory address , or b true 19 “id”; e.g., this one has id = 22. c 'y' 20 i 13 21 d 3.14 "Go" 22 s 22 23 24 … … 7 January 2019 OSU CSE 18

  19. Getting to the “Real Picture” Variables Objects These numbers are the … … “id”s of the objects. b true 19 c 'y' 20 i 13 21 d 3.14 "Go" 22 s 22 23 24 … … 7 January 2019 OSU CSE 19

  20. Getting to the “Real Picture” Variables Objects The reference value of a … … reference variable is the memory address, or “id”, of the object to b true 19 which it refers. c 'y' 20 i 13 21 d 3.14 "Go" 22 s 22 23 24 … … 7 January 2019 OSU CSE 20

  21. Getting to the “Real Picture” The reference value of a Variables Objects reference variable as a number , … … i.e., the address or “id” of the b true 19 object it refers to, is immaterial; so, this situation ... c 'y' 20 i 13 21 d 3.14 "Go" 22 s 22 23 24 … … 7 January 2019 OSU CSE 21

  22. Getting to the “Real Picture” ... is indistinguishable in a Variables Objects program from this one. (Yet in C or C++, you may do … … calculations with these b true 19 addresses/“id”s as if they were numbers! How crazy is that?) c 'y' 20 i 13 21 d 3.14 22 s "Go" 23 23 24 … … 7 January 2019 OSU CSE 22

  23. Getting to the “Real Picture” Variables Objects Similarly, it is immaterial to the … … program exactly where each b true 19 variable is located in memory. 20 i 13 21 s 23 22 c 'y' "Go" 23 d 3.14 24 … … 7 January 2019 OSU CSE 23

  24. So We Can Simplify... Variables Objects … true 19 'y' 20 13 21 3.14 22 23 "Go" 23 24 … 7 January 2019 OSU CSE 24

  25. So We Can Simplify... In our pictures, the types of Variables variables are abstracted as Objects different shapes; all … true reference variables use an 19 'y' equilateral triangle. 20 13 21 3.14 22 23 "Go" 23 24 … 7 January 2019 OSU CSE 25

  26. So We Can Simplify... Variables Variables in the program Objects are somewhere in this part … true of memory. 19 'y' 20 13 21 3.14 22 23 "Go" 23 24 … 7 January 2019 OSU CSE 26

  27. So We Can Simplify... Objects in the program are Variables Objects somewhere in this part of true memory. 'y' 13 3.14 "Go" 23 23 7 January 2019 OSU CSE 27

  28. So We Can Simplify... In our pictures, the connection between the Variables Objects reference value of a reference variable and its true object value is abstracted 'y' as an arrow. 13 3.14 "Go" 7 January 2019 OSU CSE 28

  29. Finally... Our pictures allow us to abstract away even the fact that there are two parts of true memory. 'y' 13 3.14 "Go" 7 January 2019 OSU CSE 29

  30. Notation • We never care about writing down the reference value of a reference variable as a particular numerical value (though we draw a picture of it: an arrow out of a triangle) – So, if you see something like s = "Go" in a contract or a tracing table, it must mean that the object value of s is the mathematical model value "Go" 7 January 2019 OSU CSE 30

  31. Notation • In a tracing table, however, we might want to remind ourselves there is a reference involved, so we might record the value of variable s using a right arrow instead of an equals sign, e.g., s ➞ "Go" – This means that s is a reference variable whose object value is "Go" – Or: s refers to an object with value "Go" – Why would we do this? Coming up... 7 January 2019 OSU CSE 31

  32. The Assignment Operator • The assignment operator = copies the value of the expression on the right-hand side into the variable on the left-hand side • For primitive types, “the value of” can mean only one thing • For reference types, it could mean “the reference value of” or “the object value of” – Which is it? 7 January 2019 OSU CSE 32

  33. Assignment for Primitive Types • Consider: int i = k + 7; – First, the expression k + 7 is evaluated; say k = 3 , so the expression evaluates to 10 – Next, the value 10 is copied into i , so after the above statement has finished executing, we have i = 10 • How does this happen? 7 January 2019 OSU CSE 33

  34. Step by Step: int i = k + 7; 3 We already have k , a primitive variable whose value is 3 . 7 January 2019 OSU CSE 34

  35. Step by Step: int i = k + 7; 3 7 The int literal is an anonymous primitive variable whose value is 7 . 7 January 2019 OSU CSE 35

  36. Step by Step: int i = k + 7; 3 7 10 The int addition operator + results in another anonymous primitive variable whose value is 10 . 7 January 2019 OSU CSE 36

  37. Step by Step: int i = k + 7; 3 7 10 The declaration of the int variable i results ? in an uninitialized primitive variable . 7 January 2019 OSU CSE 37

  38. Step by Step: int i = k + 7; 3 7 10 The assignment operator copies the 10 value of the right- hand side into i . 7 January 2019 OSU CSE 38

  39. Step by Step: int i = k + 7; 3 The temporary anonymous primitive variables disappear 10 now that the statement has completed executing. 7 January 2019 OSU CSE 39

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend