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

references
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

References

7 January 2019 OSU CSE 1

slide-2
SLIDE 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

slide-3
SLIDE 3

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 3

There is no limit on the number of other user-defined types that can be developed.

slide-4
SLIDE 4

Categories of Types, v. 1

7 January 2019 OSU CSE 4

boolean char int double (plus 4

  • thers)

String XMLTree ... SimpleReader SimpleWriter NaturalNumber ... Reference Types Primitive Types

slide-5
SLIDE 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

slide-6
SLIDE 6

Examples

7 January 2019 OSU CSE 6

true 'y' 13 3.14 "Go"

slide-7
SLIDE 7

Examples

7 January 2019 OSU CSE 7

true 'y' 13 3.14 "Go"

slide-8
SLIDE 8

Recall We Said Earlier...

7 January 2019 OSU CSE 8

This is a String variable s whose value is "Go", i.e., s = "Go" "Go"

slide-9
SLIDE 9

... But Here’s the “Real Picture”!

7 January 2019 OSU CSE 9

"Go"

slide-10
SLIDE 10

... But Here’s the “Real Picture”!

7 January 2019 OSU CSE 10

There is a String variable s, whose value is a reference to an object whose value is "Go". "Go"

slide-11
SLIDE 11

References and Objects

7 January 2019 OSU CSE 11

This is the reference s. "Go"

slide-12
SLIDE 12

References and Objects

7 January 2019 OSU CSE 12

This is the object s “points to”

  • r “refers to”.

"Go"

slide-13
SLIDE 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

slide-14
SLIDE 14

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 14

Think of the reference value as simply an “id” or “serial number”

  • f some place in memory.
slide-15
SLIDE 15

Getting to the “Real Picture”

7 January 2019 OSU CSE 15

Variables Objects

b c i d s true 'y' 13 3.14 22 "Go" 19 20 21 22 23 24 … … … …

slide-16
SLIDE 16

Getting to the “Real Picture”

7 January 2019 OSU CSE 16

Variables Objects

b c i d s true 'y' 13 3.14 22 "Go" 19 20 21 22 23 24 … … … … This part of memory holds:

  • values of primitive variables
  • reference values of reference

variables

slide-17
SLIDE 17

Getting to the “Real Picture”

7 January 2019 OSU CSE 17

Variables Objects

b c i d s true 'y' 13 3.14 22 "Go" 19 20 21 22 23 24 … … … … This part of memory holds:

  • object values of reference

variables

slide-18
SLIDE 18

Getting to the “Real Picture”

7 January 2019 OSU CSE 18

Variables Objects

b c i d s true 'y' 13 3.14 22 "Go" 19 20 21 22 23 24 … … … … Each object in memory has a unique memory address, or “id”; e.g., this one has id = 22.

slide-19
SLIDE 19

Getting to the “Real Picture”

7 January 2019 OSU CSE 19

Variables Objects

b c i d s true 'y' 13 3.14 22 "Go" 19 20 21 22 23 24 … … … … These numbers are the “id”s of the objects.

slide-20
SLIDE 20

Getting to the “Real Picture”

7 January 2019 OSU CSE 20

Variables Objects

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

slide-21
SLIDE 21

Getting to the “Real Picture”

7 January 2019 OSU CSE 21

Variables Objects

b c i d s true 'y' 13 3.14 22 "Go" 19 20 21 22 23 24 … … … … The reference value of a reference variable as a number, i.e., the address or “id” of the

  • bject it refers to, is immaterial;

so, this situation ...

slide-22
SLIDE 22

Getting to the “Real Picture”

7 January 2019 OSU CSE 22

Variables Objects

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

slide-23
SLIDE 23

Getting to the “Real Picture”

7 January 2019 OSU CSE 23

Variables Objects

b c i d s true 'y' 13 3.14 23 "Go" 19 20 21 22 23 24 … … … … Similarly, it is immaterial to the program exactly where each variable is located in memory.

slide-24
SLIDE 24

So We Can Simplify...

7 January 2019 OSU CSE 24

Variables Objects

"Go" 19 20 21 22 23 24 … … true 'y' 13 3.14 23

slide-25
SLIDE 25

So We Can Simplify...

7 January 2019 OSU CSE 25

Variables Objects

"Go" 19 20 21 22 23 24 … … true 'y' 13 3.14 23 In our pictures, the types of variables are abstracted as different shapes; all reference variables use an equilateral triangle.

slide-26
SLIDE 26

So We Can Simplify...

7 January 2019 OSU CSE 26

Variables Objects

"Go" 19 20 21 22 23 24 … … true 'y' 13 3.14 23 Variables in the program are somewhere in this part

  • f memory.
slide-27
SLIDE 27

So We Can Simplify...

7 January 2019 OSU CSE 27

Variables Objects

true 'y' 13 3.14 23 "Go" Objects in the program are somewhere in this part of memory. 23

slide-28
SLIDE 28

So We Can Simplify...

7 January 2019 OSU CSE 28

Variables Objects

true 'y' 13 3.14 "Go" In our pictures, the connection between the reference value of a reference variable and its

  • bject value is abstracted

as an arrow.

slide-29
SLIDE 29

Finally...

7 January 2019 OSU CSE 29

true 'y' 13 3.14 "Go" Our pictures allow us to abstract away even the fact that there are two parts of memory.

slide-30
SLIDE 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

slide-31
SLIDE 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

slide-32
SLIDE 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

slide-33
SLIDE 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

slide-34
SLIDE 34

Step by Step: int i = k + 7;

7 January 2019 OSU CSE 34

3 We already have k, a primitive variable whose value is 3.

slide-35
SLIDE 35

Step by Step: int i = k + 7;

7 January 2019 OSU CSE 35

7 The int literal is an anonymous primitive variable whose value is 7. 3

slide-36
SLIDE 36

Step by Step: int i = k + 7;

7 January 2019 OSU CSE 36

7 The int addition

  • perator + results in

another anonymous primitive variable whose value is 10. 3 10

slide-37
SLIDE 37

Step by Step: int i = k + 7;

7 January 2019 OSU CSE 37

7 The declaration of the int variable i results in an uninitialized primitive variable. 3 10 ?

slide-38
SLIDE 38

Step by Step: int i = k + 7;

7 January 2019 OSU CSE 38

7 3 10 10 The assignment

  • perator copies the

value of the right- hand side into i.

slide-39
SLIDE 39

Step by Step: int i = k + 7;

7 January 2019 OSU CSE 39

The temporary anonymous primitive variables disappear now that the statement has completed executing. 3 10

slide-40
SLIDE 40

A Tracing Table

7 January 2019 OSU CSE 40

Code State

k = 3 int i = k + 7; k = 3 i = 10

slide-41
SLIDE 41

Assignment for Reference Types

  • Consider:

String s = t + "io"; – First, the expression t + "io" is evaluated; say t = "Oh", so the expression evaluates to "Ohio" – Next, the value "Ohio" is copied into s, so after the above statement has finished executing, we have s = "Ohio"

  • How does this happen?

7 January 2019 OSU CSE 41

slide-42
SLIDE 42

Step by Step: String s = t + "io";

7 January 2019 OSU CSE 42

"Oh" We already have t, a reference variable whose object value is "Oh".

slide-43
SLIDE 43

7 January 2019 OSU CSE 43

"Oh" "io" The String literal is an anonymous reference variable whose object value is "io".

Step by Step: String s = t + "io";

slide-44
SLIDE 44

7 January 2019 OSU CSE 44

"Oh" "io" "Ohio" The String concatenation

  • perator + results in another

anonymous reference variable whose object value is "Ohio".

Step by Step: String s = t + "io";

slide-45
SLIDE 45

7 January 2019 OSU CSE 45

"Oh" ? "io" "Ohio" The declaration of the String variable s results in an uninitialized reference variable.

Step by Step: String s = t + "io";

slide-46
SLIDE 46

7 January 2019 OSU CSE 46

"Oh" "io" "Ohio" The assignment

  • perator copies the

reference value on the right-hand side into s.

Step by Step: String s = t + "io";

slide-47
SLIDE 47

7 January 2019 OSU CSE 47

"Oh" "Ohio" The temporary anonymous reference variables disappear now that the statement has completed executing — but the

  • bjects with values "io"

and "Ohio" remain! "io"

Step by Step: String s = t + "io";

slide-48
SLIDE 48

7 January 2019 OSU CSE 48

"Oh" "Ohio" Java has a garbage collector that may come along later and “reclaim” or “recycle” the memory where an unreferenced temporary object is stored; but this does not affect our reasoning.

Step by Step: String s = t + "io";

slide-49
SLIDE 49

A Tracing Table Using ➞

7 January 2019 OSU CSE 49

Code State

t ➞ "Oh" String s = t + "io"; t ➞ "Oh" s ➞ "Ohio"

slide-50
SLIDE 50

A Tracing Table Using =

7 January 2019 OSU CSE 50

Code State

t = "Oh" String s = t + "io"; t = "Oh" s = "Ohio"

slide-51
SLIDE 51

So What’s Different?

  • It seems the net effect of assignment is

essentially the same whether we have primitive variables or reference variables

  • But not quite...

7 January 2019 OSU CSE 51

slide-52
SLIDE 52

Simplest Assignment: Primitive

  • Consider:

int i = k; – First, the expression k is evaluated; say k = 3, so the expression evaluates to 3 – Next, the value 3 is copied into i, so after the above statement has finished executing, we have i = 3

  • Let’s do this step-by-step as well...

7 January 2019 OSU CSE 52

slide-53
SLIDE 53

Step by Step: int i = k;

7 January 2019 OSU CSE 53

3 We already have k, a primitive variable whose value is 3.

slide-54
SLIDE 54

Step by Step: int i = k;

7 January 2019 OSU CSE 54

The declaration of the int variable i results in an uninitialized primitive variable. 3 ?

slide-55
SLIDE 55

Step by Step: int i = k;

7 January 2019 OSU CSE 55

3 3 The assignment

  • perator copies the

value of the right- hand side into i.

slide-56
SLIDE 56

Step by Step: int i = k;

7 January 2019 OSU CSE 56

3 3 Note there are now two independent copies of the value 3,

  • ne in each variable.
slide-57
SLIDE 57

A Tracing Table

7 January 2019 OSU CSE 57

Code State

k = 3 int i = k; k = 3 i = 3

slide-58
SLIDE 58

Simplest Assignment: Reference

  • Consider:

String s = t; – First, the expression t is evaluated; say t = "Oh", so the expression evaluates to "Oh" – Next, the value "Oh" is copied into s, so after the above statement has finished executing, we have s = "Oh"

  • Let’s do this step-by-step as well...

7 January 2019 OSU CSE 58

slide-59
SLIDE 59

Step by Step: String s = t;

7 January 2019 OSU CSE 59

"Oh" We already have t, a reference variable whose object value is "Oh".

slide-60
SLIDE 60

7 January 2019 OSU CSE 60

"Oh" ? The declaration of the String variable s results in an uninitialized reference variable.

Step by Step: String s = t;

slide-61
SLIDE 61

7 January 2019 OSU CSE 61

"Oh" The assignment

  • perator copies the

reference value on the right-hand side into s.

Step by Step: String s = t;

slide-62
SLIDE 62

7 January 2019 OSU CSE 62

"Oh" Notice there is still

  • nly one object but

now two references to it! These references are called aliases.

Step by Step: String s = t;

slide-63
SLIDE 63

A Tracing Table Using ➞

7 January 2019 OSU CSE 63

Code State

t ➞ "Oh" String s = t; s,t ➞ "Oh"

slide-64
SLIDE 64

A Tracing Table Using ➞

7 January 2019 OSU CSE 64

Code State

t ➞ "Oh" String s = t; s,t ➞ "Oh" The arrow notation helps us remember that there is only one

  • bject but two

references to it.

slide-65
SLIDE 65

A Tracing Table Using =

7 January 2019 OSU CSE 65

Code State

t = "Oh" String s = t; t = "Oh" s = "Oh"

slide-66
SLIDE 66

A Tracing Table Using =

7 January 2019 OSU CSE 66

Code State

t = "Oh" String s = t; t = "Oh" s = "Oh" Is this tracing table OK? It suggests there are two separate objects with the value "Oh".

slide-67
SLIDE 67

Why It Matters: NaturalNumber

7 January 2019 OSU CSE 67

Code State

z = 99 NaturalNumber n = z; z = 99 n = 99 n.increment(); z = 99 n = 100

slide-68
SLIDE 68

Why It Matters: NaturalNumber

7 January 2019 OSU CSE 68

Code State

z = 99 NaturalNumber n = z; z = 99 n = 99 n.increment(); z = 99 n = 100 But this is not what really happens! Try it, step-by-step...

slide-69
SLIDE 69

Why It Matters: NaturalNumber

7 January 2019 OSU CSE 69

Code State

z ➞ 99 NaturalNumber n = z; z,n ➞ 99 n.increment(); z,n ➞ 100

slide-70
SLIDE 70

Why It Matters: NaturalNumber

7 January 2019 OSU CSE 70

Code State

z ➞ 99 NaturalNumber n = z; z,n ➞ 99 n.increment(); z,n ➞ 100 This is what really happens!

slide-71
SLIDE 71

An Important Claim

  • The problem illustrated here that arises

from aliasing of references with NaturalNumber cannot happen with String or XMLTree

  • What’s the difference?

7 January 2019 OSU CSE 71

slide-72
SLIDE 72

Immutable vs. Mutable Types

  • Java reference types are further divided

into two different categories:

– Types for which no method might change the value of the receiver, or any other argument of that type, are called immutable types – Types for which at least one method might change the value of the receiver, or some

  • ther argument of that type, are called

mutable types

7 January 2019 OSU CSE 72

slide-73
SLIDE 73

Categories of Types, v. 2

7 January 2019 OSU CSE 73

boolean char int double String XMLTree ... SimpleReader SimpleWriter NaturalNumber ... Reference Types Primitive Types Immutable Types Mutable Types

slide-74
SLIDE 74

Restated Claim

  • You may reason about immutable

types/variables as if they were primitive

  • If and only if there are aliased references,

you may not reason about mutable types/variables as if they were primitive

– ... because this reasoning short-cut is unsound, i.e., it may predict wrong results compared to executing the code

7 January 2019 OSU CSE 74

slide-75
SLIDE 75

For Reasoning, It Might As Well Be...

7 January 2019 OSU CSE 75

boolean char int double String XMLTree ... SimpleReader SimpleWriter NaturalNumber ... Reference Types Primitive Types Immutable Types Mutable Types

slide-76
SLIDE 76

Why Have Mutable Types?

  • Couldn’t designers of new types just

always make them immutable, to simplify reasoning?

– Yes, but there would be serious efficiency penalties in many cases, so best practices dictate that it is more practical to allow mutable types and be especially careful to limit aliasing of references

7 January 2019 OSU CSE 76

slide-77
SLIDE 77

Parameter Passing for References

  • Just as the assignment operator copies

reference values, parameter passing to method calls copies reference values

– The reference values of the arguments are copied into the formal parameters to initialize them at the time of the call – Upon return, nothing is copied back except the returned value of the method (if any), and here too the reference value is copied back

7 January 2019 OSU CSE 77

slide-78
SLIDE 78

Complete This Table

7 January 2019 OSU CSE 78

Code State

m ➞ 143 k ➞ 70 m.transferFrom(k);

slide-79
SLIDE 79

Complete This Table

7 January 2019 OSU CSE 79

Code State

m ➞ 143 k ➞ 70 m.transferFrom(k); Consult the contract for transferFrom.

slide-80
SLIDE 80

Equality Checking for References

  • Just as the assignment operator = copies

reference values, and parameter passing to method calls copies reference values, the equality operator == compares reference values

7 January 2019 OSU CSE 80

slide-81
SLIDE 81

Equality Checking for References

  • Since comparing object values is often

what you want instead, the equals method compares object values

– At least, best practices say it is supposed to – Beware: though the equals method does what it is supposed to do for nearly all types in the Java libraries (and certainly for all types in the OSU CSE components), in some cases it, too, simply compares reference values!

7 January 2019 OSU CSE 81

slide-82
SLIDE 82

Example with NaturalNumber

7 January 2019 OSU CSE 82

Code State

m ➞ 52 k ➞ 52 boolean b = (m == k); m ➞ 52 k ➞ 52 b = false

slide-83
SLIDE 83

Example with NaturalNumber

7 January 2019 OSU CSE 83

Code State

m,k ➞ 52 boolean b = (m == k); m,k ➞ 52 b = true

slide-84
SLIDE 84

Example with NaturalNumber

7 January 2019 OSU CSE 84

Code State

m ➞ 52 k ➞ 52 boolean b = m.equals(k); m ➞ 52 k ➞ 52 b = true

slide-85
SLIDE 85

Example with NaturalNumber

7 January 2019 OSU CSE 85

Code State

m,k ➞ 52 boolean b = m.equals(k); m,k ➞ 52 b = true

slide-86
SLIDE 86

Resources

  • Wikipedia: Pointer (computer programming)

– http://en.wikipedia.org/wiki/Pointer_(computer_programming)

7 January 2019 OSU CSE 86