References and pointers do the same thing as I know. Is there any difference between them?
If there is no difference, why we call them reference not pointer?
References and pointers do the same thing as I know. Is there any difference between them?
If there is no difference, why we call them reference not pointer?
Broadly, you can make the following distinction:
A reference is a variable that refers to something else and can be used as an alias for that something else.
A pointer is a variable that stores a memory address, for the purpose of acting as an alias to what is stored at that address.
So, a pointer is a reference, but a reference is not necessarily a pointer.
Pointers are a particular implementation of the concept of a reference, and the term tends to be used only for languages that give you direct access to the memory address.
References can be implemented internally in a language using pointers, or using some other mechanism. For example, Perl has symbolic references, which refer to the variable by name rather than memory location. Which is why weird code like this works:
my $bar = "foo";
$$bar = "Am I foo or bar?"; #This is actually a reference to $foo!
print $foo;
This distinction between references and pointers is not a hard and fast rule (C++ being the main exception, because it has both references and pointers, with a distinct meaning for each) but it covers most of the usage.
this
to be a special variable. But I suppose that's a matter of opinion--the definition of "variable" would be a whole other question :)
There is no generally accepted difference between pointers and references if you look at a wide enough distribution of languages using the terms. Variant of the same concepts use both terms (and some other like access) nearly indifferently, yet some characteristics tend to be closely associated with one of the term:
if arithmetic is available, I've never seen the term reference used. (But pointer doesn't imply that arithmetic is available)
if the referenced object can't be changed, I've never seen the term pointer used excepted when there is a constant property which can be applied to the pointer as well as to other things. (But reference doesn't imply non rebindability).
Pointers and references are placeholder to use objects.
Instead of using the object itself you can use a pointer or reference to access and/or manipulate the object.
Car myCar = new Car();
Car& myCarReference = myCar;
Changing the content of myCarReference will change the content of myCar.
Pointers add powerful but potentially dangerous address operations to references. For example:
Car* myCarPointer;
// 0x12345678=the memory adress where one car
// object is supposed to be in memory.
myCarPointer = 0x12345678;
myCarPointer->setColor(red);
myCarPointer+=20; // advance 20 cars in an array
Potentially dangerous means "you can assign any address to a pointer. The pointer can point to a location where there is no car object.
Languages like java and c# introduced references to make programms more robust compared to c/c++ which have only pointers
You are juxtaposing the wrong things. In C++, pointers and references have different meanings, but because you aren't asking about C++, I suspect that isn't what you are after. If you are, check out Harssh's answer.
What you probably want to know is the difference between a reference to an object and the object itself. In most newer languages you can pass an object around by value or by reference.
Passing an object by value means making a copy of it. You can modify that copy without affecting the original. Making that copy can cost a lot of memory access though.
Passing an object by reference means passing a handle to that object. This is cheaper because you don't need to make a copy. It also means that any changes you make will affect the original.
Functional programming languages will tend to pass everything by value because that avoids side effects.
Pointers and references look different enough (pointers use the * and -> operators, references use .), but they seem to do similar things. Both pointers and references let you refer to other objects indirectly. How, then, do you decide when to use one and not the other? First, recognize that there is no such thing as a null reference. A reference must always refer to some object. As a result, if you have a variable whose purpose is to refer to another object, but it is possible that there might not be an object to refer to, you should make the variable a pointer, because then you can set it to null. On the other hand, if the variable must always refer to an object, i.e., if your design does not allow for the possibility that the variable is null, you should probably make the variable a reference.
for more refer http://www.cplusplus.com/articles/ENywvCM9/