1 / 9

Value Types and Reference Types

In Java . . . Value Types and Reference Types. Value Types. Variables contain the values of primitive data types. Value Types. In Java, primitive data types such as int , float , double , char , and boolean are called value types

slone
Download Presentation

Value Types and Reference Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. In Java . . . Value Types and Reference Types

  2. Value Types Variables contain the values of primitive data types

  3. Value Types • In Java, primitivedatatypes such as int, float, double, char, and boolean are called valuetypes • A variable of one of these types is a container for onevalue of its appropriatetype Value types of variables contain their values

  4. 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

  5. Reference Types in Java Objects of Classes

  6. Reference Types • A variable declared as a reference to an object of some class (either a built-inclass or a user-defined class) is a reference type • A referencevariableDOES NOTcontain 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 Actual Object Reference

  7. Reference Types personA null personB null new creates an actual object of type Person initialized to Rachel Walling Rachel Walling personA personB Two different references now refer to a single actual object

  8. 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 Smith After line 5 bob

  9. 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 Since nothing refers to this object now, it is destroyed (“garbage collected”) by Java Bob Smith bob Bob Jones After line 7

More Related