educative.io

Values and Reference - Data type

The fragment below was taken from the link below. My question is: is the paper, used as an example, an OBJECT or a VARIABLE? And what is its name? In the first fragment the paper holds the value of the information written on it and the second example the paper holds an information that makes a reference of something.
Also I would like to know if a reference could be a value, as it is written below.

Everithing can be found in the link below: Data Types

Thank you

Leandro

FRAGMENT

"(…) Imagine you have a piece of paper. You want the paper to hold some information, such as your name. You can write your name on the piece of paper; therefore, the value of the paper is the same as the information it provides. This is an example of value type . If someone wants to know your name, all they have to do is read the paper.

Now imagine, you want the paper to hold your house. That’s not physically possible, so you write the address to your house, hence the value of the paper is a reference to the required information. This is an example of reference type . If someone wants to go to your house, they will first have to read the address on the paper and then get directions to your house. (…)"

Hey @Leandro_Safra

Thank You for reaching out to the Educative team.

As to the question, in the provided fragment, the expression “paper” is utilized as an analogy to explain the concepts of value types and reference types in programming. In this analogy:

  1. At the point when the paper holds your name, it is an example of a value type. The paper itself directly contains the information (your name), and someone can retrieve that information by simply reading the paper. “Paper” may be considered a variable in this instance.
  2. At the point when the paper holds the location to your home, it is an example of a reference type. In this scenario, the paper contains a reference (address) to the location of the information, which is the location of your house, but not the information itself. This is similar to how programming languages use reference types. In this case, “paper” can also be considered a variable.

In programming, a variable is a named storage location that can hold a value. The “paper” is like a variable in the analogy, which can hold a value directly or a reference to another piece of information.

To respond directly to your inquiries:

  • The “paper” can be viewed as both an object and a variable, depending on whether it is used to hold a direct value or a reference.
  • In terms of programming, the analogy does not specifically mention the name of the “paper,” but in a programming context, that name would be the variable used to refer to that particular piece of storage.
  • A reference can be considered a value, but it’s a value that points to another location in memory where the actual data is stored. In the analogy, the address on the paper is the reference, and it serves as a value that leads to the actual information (the location of your house).

I hope it helps. Happy Learning :blush:

1 Like