Difference Between a Variable, Object, and Reference

What is the difference between a variable and a reference in C++?

Here is a way to understand the difference:

  • Objects that can change state are also called "variables".
  • Pointers are objects (variable or not). They have a state.
  • References are "nicknames". They don't have a state, but expose the state of the refereed object (which is why you can't re-assign a reference, it's not an actual object).

Now, in some cases references might be implemented as pointers, but from the language point of view, references are just not pointers, they really are additional names for an object already existing.

As pointers are objects, and have a state, passing pointers to functions will copy that state, the pointer's state, not the pointee's state. However, references have no state, so if you pass a reference to a function, it's the refereed object that you pass (by copy).

By observation, the use of variables names and references is
exchangeable. Is that true?

"References are nickname" is the best way to understand references.

What is the difference between passing a variable name as parameter
and passing a reference? e.g.,

void func(int a); vs void func2(int& b);

The first implementation ask for a copy of the object passed. That is, internally func() can do anything to a, without changing the object that was passed to func() because internally func() made a copy of that object and manipulates the copy, not the original.

The second implementation ask for "a nickname for an object already existing". First, the object have to exist and if passed, a nickname for it will be created inside the function. That nickname, the reference b, is still a nickname for the original object. This mean that any manipulation done to b will affect the original object passed to func2().

  • func() signature says "I need this data but I will not modify the original object passed.".
  • func2() signature says "I need an object that I WILL certainly modify, pass it so that I can modify it".

Bonus stage:

At this point, if you don't know yet about const, that might be useful: in function signatures const is used with references to specify the arguments that are "read-only".

Let me clarify:

void func3( const int& b);

Here func3 says: "I need to access to an object, but really I will not make a copy of it. However I guarantee that I will not change that object".

So, why would we need that? Because some objects are expensive to copy. int is cheap to copy so most people will just pass it and func() and func3() are basically equivalent (depends on implementation but generally true).

If however we want to pass, says, a very big object, like a data buffer, we really don't want to copy it again and again just to apply some algorithms.
So we do want to pass it by reference. However, depending on the function, sometime you want to extract information and work with it, so you only need "read-only" access to the argument. In this case you use const Object&. However, if you need to apply the algorithm to the object passed, you need to be able to modify it, which you could call "write-access". In this case, you need to use a normal reference.
Asking for a copy basically mean that you want to manipulate an object that is the same state than the passed object, but is not the passed object.

To summarize:

  • func( T object ) : I want to have a copy of an object of type T;
  • func( T& object ) : I want to have "write-access" to an object of type T - assume that I will modify that object!;
  • func( const T& object ) or func( T const & object ) // which are the same : I want to read the state of an object, but I guarantee you that I will not modify it, I want "read-only" access.

Actually, the "read-only" guarantee could be violated using const_cast<> but that's a different story and it's only used in some very very very narrow cases.

Last thing you need to know is that if you have a member function, then you can do:

class K{
public:
void func() const; // see the const?
};

In this specific case, what you say is that inside the function, which is basically equivalent to:
void func( const K* this );

In this case you can see that this is a pointer but it's pointing to a const object. This mean that func() guarantee that the object it is member of (this) is never modified through this function (except some specific cases, see mutable keyword, another long story).

What is Object Reference Variable?

I'm not sure I have the elegance to properly answer this, but...

  • An Object is an instance of a Class, it is stored some where in memory
  • A reference is what is used to describe the pointer to the memory location where the Object resides.
  • A variable is a means by which you can access that memory location within your application (its value is "variable"). While a variable can only point to a single memory address (if its not null), it may change and point to different locations through out the life cycle of the application

What is the difference between an Object , Reference ID , and Reference Variable in Java?

An object is, essentially, a chunk of memory living in the heap. (Part of the memory structure of objects includes a reference to the class of that object.)

Object variables in Java (like e, in this example) contain references to objects living in the heap.

Classes are completely different from all of these; they might describe the structure of objects of that type, and have method implementations and the like, but classes live in an entirely different area of memory from other objects.

What is difference between references and objects in java?

References are names. Objects are stuff. You can have different names for stuff, even for stuff that doesn't actually exist.

You can declare names, without actually giving them any "real" meaning, like this:

GUI g1;

You can assign meaning (real stuff to refer to) to names with the = operator:

GUI g1 = some_gui;

Names can change their meaning over time. The same name can refer to different things at different points in history.

GUI g1 = some_gui;

doSomething();

g1 = some_other_gui;

There are also synonyms: multiple names can refer to the same thing:

GUI g2 = g1;

That's pretty much what references do. They are names meant to refer to stuff.

Stuff can be created:

new GUI();

Stuff can be created and named on the spot for later reference (literally!):

GUI g1 = new GUI();

And stuff can be referred to, using its name (or any of its names!):

g1.doSomething();
g2.doSomethingAgain();

Different stuff of the same kind (Class) can be created, and named differently:

GUI g1 = new GUI();
GUI g2 = new GUI();
GUI g3 = new GUI();

GUI g1_synonym = g1;

:)

what is the actual difference between reference variable and object

instance1 is a variable.

Because its type is a reference type, it is a reference to an object instance that lives on the heap.

new SampleClass() is a constructor call that creates a new object on the heap and returns a reference to it.



Related Topics



Leave a reply



Submit