in java.... variables contain the values of primitive data types value types

9
VALUE TYPES AND REFERENCE TYPES In Java . . .

Upload: nathaniel-hawkins

Post on 20-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: In Java.... Variables contain the values of primitive data types Value Types

VALUE TYPES AND REFERENCE TYPES

In Java . . .

Page 2: In Java.... Variables contain the values of primitive data types Value Types

Variables contain the values of primitive data types

Value Types

Page 3: In Java.... Variables contain the values of primitive data types Value Types

Value Types In Java, primitive data types such as int, float,

double, char, and boolean are called value types

A variable of one of these types is a container for one value of its appropriate type

counter price foundIt

7 5.99 false

Value types of variables contain

their values

Page 4: In Java.... Variables contain the values of primitive data types Value Types

Value Types Because a variable of a value type is a

container for ONE value of the corresponding type, giving it a second value replaces the first value entirely with the second

dependents

2

dependents

2 3

Page 5: In Java.... Variables contain the values of primitive data types Value Types

Objects of Classes

Reference Types in Java

Page 6: In Java.... Variables contain the values of primitive data types Value Types

Reference Types A variable declared as a reference to an object

of some class (either a built-in class or a user-defined class) is a reference type

A reference variable DOES NOT contain the the object to which it refers – instead, it refers to that object (i.e., it points to that object)

A reference variable is null (refers to nothing) until it is given an object for it to reference

Reference Actual Object

Page 7: In Java.... Variables contain the values of primitive data types Value Types

Reference Typesperson

Aperson

B

null

null

personA

personB

Rachel Walling

new creates an actual object of

type Person initialized to

Rachel Walling

Two different references now refer to a single actual

object

Page 8: In Java.... Variables contain the values of primitive data types Value Types

Reference types Just as value types may only have one value at

a time, reference types may only refer to one actual instance at a time

bob Bob

SmithAfter line 5

Page 9: In Java.... Variables contain the values of primitive data types Value Types

Reference types As value types may only have one value at a

time, reference types may only refer to one actual instance at a time

bob Bob

Smith

Bob Jones

Since nothing refers to this object now, it is destroyed (“garbage collected”)

by Java

After line 7