What is the difference between a variable, an object, and a link?

What are the differences between variables , objects, and references ?

For example: they all point to some type, and all of them must contain values ​​(unless, of course, you have a temporary type with a zero value), but exactly how do their functions and implementations differ from each other?

Example:

Dog myDog = new Dog(); //variable myDog that holds a reference to object Dog int x = 12; //variable x that hold a value of 12 

They have the same concepts, but how do they differ?

+61
java variables object reference c #
Aug 14 '15 at 12:30
source share
5 answers

(Just to be clear, the explanation I am giving here is for Java and C #. Do not assume this applies to other languages, although there may be bits.)

I like to use the analogy when telling someone where I live. I can write my address on a piece of paper:

  • Variable like a sheet of paper. It contains meaning, but it is not meaning itself. You can cross out everything there and write something else.
  • The address that I write on a piece of paper looks like a link. This is not my home, but it is a way of navigating to my home.
  • My house itself is like an object. I can give several references to the same object, but there is only one object.

Does it help?

The difference between a value type and a reference type is what is written on a piece of paper. For example, here:

 int x = 12; 

looks like a piece of paper with number 12 written directly on it. Pay attention to:

 Dog myDog = new Dog(); 

It does not record the content of the Dog object itself on a piece of paper - it creates a new Dog , and then writes a link to the dog on this paper.

In terms of non-analogy:

  • A variable is a storage location in memory. It has a name with which you can reference it at compile time, and at run time it has a value that will always be compatible with its type of compilation time. (For example, if you have a Button variable, the value will always be a reference to an object of type Button or some subclass or null reference.)
  • An object is a kind of separate object. It is important to note that the value of a variable or any expression is never an object, but only a reference. The facility effectively consists of:
    • Fields (state)
    • Type reference (can never change over the lifetime of an object)
    • Monitor (for synchronization)
  • A reference is a value used to access an object - for example, call methods on it, access fields, etc. You usually navigate a link using an operator . . For example, if foo is a Person variable, foo.getAddress().getLength() takes the value foo (link) and calls getAddress() object referenced by this link. The result could be a String reference ... then we call getLength() on the object that this link refers to.
+142
Aug 14 '15 at 12:33
source share

I often use the following analogy to explain these concepts.




Imagine the object is a balloon. A variable is a person. Each person is either in a value type command, or in a reference type command. And they all play a little game with the following rules:

Rules for value types:

  • You are holding a balloon filled with air. (Variables of type value store the object.)
  • You should always keep one balloon. (Value types cannot be null.)
  • When someone wants your balloon, they can explode their own identical and hold it in their hands. (In value types, the object is copied.)
  • Two people cannot hold the same balloon. (Value types are not separated.)
  • If you want to hold another balloon, you have to pull out the one you are already holding and grab another. (An object of type value is destroyed when replaced.)

Rules for reference types:

  • You can hold a piece of cord that leads to a balloon filled with helium. (Reference variable types store a reference to an object.)
  • You are allowed to hold one part of a line or not contain a single line at all. (Reference type variables are NULL.)
  • When someone wants your balloon, they can get their own cord and tie it to the same balloon that you have. (In reference types, the link is copied.)
  • Several people can hold pieces of string that all lead to the same balloon. (Reference type objects can be shared.)
  • As long as there is at least one person still holding the string on a particular balloon, the balloon is safe. (The reference type object is alive as long as it is available.)
  • For any particular balloon, if everyone eventually leaves it, then that balloon will fly away and no one else can reach it. (An object of a reference type may become inaccessible at some point.)
  • At some later point before the end of the game, a lost balloon may appear on its own due to atmospheric pressure. (Unreachable objects have the right to collect garbage that is not deterministic.)
+27
Aug 14 '15 at 13:46 on
source share

You can think of it as answers to questions.

An object is that ...
It is like any physical thing in the world, a “thing” that is recognizable in itself and has significant properties that distinguishes it from another “thing”. As you know, a dog is a dog because it barks, moves its tail and goes after the ball if you throw it.

A variable is ...
For example, if you look with your own hands. Each of them is the hand itself. They have fingers, nails and bones in their skin, but you know that this is your left hand and the other is your right. That is, you can have two "things" of the same type / kind, but each can be different in its own way, can have different meanings.

Link is where ...
If you look at two houses on the street, although they have their own facade, you can get to each at one unique address, that is, if you are far, like three blocks away or in another country, you can tell the address of the house, because they will still be where you left them, even if you cannot specify them directly.

Now for programming, examples in C ++ - way

 class Person{...} Person Ana = new Person(); //An object is an instance of a class(normally) 

That is, Ana is a person, but she has unique properties that distinguishes her from another person.

 &Ana //This is a reference to Ana, that is to say, a "where" does the variable //"Ana" is stored, wether or not you know it value(s) 

Ana itself is a variable for storing the properties of a person named "Ana"

+7
Aug 14 '15 at 15:26
source share

John is great for approaching him by analogy. If a more specific wording is useful to you, I can file.

Let's start with the variable. A variable is a [named] thing that contains a value. For example, int x = 3 defines a variable with the name x, which contains the integer 3. If I follow it with the destination, x=4 , x now contains the integer 4. The main thing is that we did not replace the variable. We do not have a new “variable x, whose value is now 4”, we just replaced the value of x with the new value.

Now go to the objects. Objects are useful because often you need one “thing” that can be referenced from many places. For example, if you have a document open in the editor and you want to send it to the printer, it would be nice to have only one document referenced by both the editor and the printer. This saves you having to copy it more time than you might want.

However, since you do not want to copy it more than once, we cannot just put the object in a variable. Variables are held on value, so if two variables are held on an object, they will have to make two copies, one for each variable. Links are the gaps that allow this. Links are small, easily copied values ​​that can be stored in variables.

So, in the code, when entering Dog dog = new Dog() new operator creates a new Dog object and returns a reference to this object so that it can be assigned to a variable. The assignment then assigns dog value of the reference to your newly created object.

+6
Aug 14 '15 at 21:37
source share

new Dog () will create an instance of the Dog ie object), it will create memory for the object. You need to access the variable in order to manipulate some operations. For this you need a link which is Dog myDog. If you try to print the object, it will output an unreadable value, which is nothing more than an address.

  myDog -------> new Dog(). 
+3
Aug 14 '15 at 12:34
source share



All Articles