This question already has an answer here:
Exactly what are the differences between variables, objects, and references?
For example: they all point to some type, and they must all hold values (unless of course you have the temporary null-able type), but precisely how are their functions and implementations different 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 are they different?
Jon's answer is great for approaching it from analogy. If a more concrete wording is useful for you, I can pitch in.
Let's start with a variable. A variable is a [named] thing which contains a value. For instance,
int x = 3
defines a variable named x, which contains the integer 3. If I then follow it up with an assignment,x=4
, x now contains the integer 4. The key thing is that we didn't replace the variable. We don't have a new "variable x whose value is now 4," we merely replaced the value of x with a new value.Now let's move to objects. Objects are useful because often you need one "thing" to be referenced from many places. For example, if you have a document open in an editor and want to send it to the printer, it'd be nice to only have one document, referenced both by the editor and the printer. That'd save you having to copy it more times than you might want.
However, because you don't want to copy it more than once, we can't just put an object in a variable. Variables hold onto a value, so if two variables held onto an object, they'd have to make two copies, one for each variable. References are the go-between that resolves this. References are small, easily copied values which can be stored in variables.
So, in code, when you type
Dog dog = new Dog()
, the new operator creates a new Dog Object, and returns a Reference to that object, so that it can be assigned to a variable. The assignment then givesdog
the value of a Reference to your newly created Object.new Dog() will instantiate an object Dog ie) it will create a memory for the object. You need to access the variable to manipulate some operations. For that you need an reference that is Dog myDog. If you try to print the object it will print an non readable value which is nothing but the address.
(Just to be clear, the explanation I'm giving here is specific to Java and C#. Don't assume it applies to other languages, although bits of it may.)
I like to use an analogy of telling someone where I live. I might write my address on a piece of paper:
Does that help?
The difference between a value type and a reference type is what gets written on the piece of paper. For example, here:
is like having a piece of paper with the number 12 written on it directly. Whereas:
doesn't write the Dog object contents itself on the piece of paper - it creates a new
Dog
, and then writes a reference to the dog on that paper.In non-analogy terms:
Button
variable, the value will always be a reference to an object of typeButton
or some subclass - or thenull
reference.).
operator. For example, iffoo
is aPerson
variable,foo.getAddress().getLength()
would take the value offoo
(a reference) and callgetAddress()
on the object that that reference refers to. The result might be aString
reference... we then callgetLength()
on the object that that reference refers to.You can think of it like a answering questions.
An object is a what...
It's like any physical thing in the world, a "thing" which is recognizable by itself and has significant properties that distinguishes from other "thing". Like you know a dog is a dog because it barks, move its tail and go after a ball if you throw it.
A variable is a which...
Like if you watch your own hands. Each one is a hand itself. They have fingers, nails and bones within the skin but you know one is your left hand and the other the right one. That is to say, you can have two "things" of the same type/kind but every one could be different in it's own way, can have different values.
A reference is a where...
If you look at two houses in a street, although they're have their own facade, you can get to each one by their one unique address, meaning, if you're far away like three blocks far or in another country, you could tell the address of the house cause they'll still be there where you left them, even if you cannot point them directly.
Now for programming's sake, examples in a C++ way
That is to say, Ana is a person, but she has unique properties that distinguishes her from another person.
Ana
itself is the variable for storing the properties of the person named "Ana"I often use the following analogy when explaining these concepts.
Imagine that an object is a balloon. A variable is a person. Every person is either in the value type team or in the reference type team. And they all play a little game with the following rules:
Rules for value types:
Rules for reference types: