This question already has an answer here:
Can any one tell me the exact diff between call by pointer and call by reference. Actually what is happening on both case?
Eg:
Call By Reference:
void swap(int &x, int &y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
swap(a, b);
Call By Pointer:
void swap(int *x, int *y)
{
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put x into y */
return;
}
swap(&a, &b);
Semantically these calls have identical results; under the hood references are implemented with pointers.
The important difference between using references and pointers is with references you have a lot less rope to hang yourself with. References are always pointing to "something", where as pointers can point to anything. For example, it is perfectly possible to do something like this
And now this code could do anything, crash, run, have monkeys fly out of your noes, anything.
When in doubt, prefer references. They are a higher, safer level of abstraction on pointers.
The both cases do exactly the same.
However, the small difference is, that references are never null (and inside function you are sure, that they are referencing valid variable). On the other hand, pointers may be empty or may point to invalid place in memory, causing an AV.
Your code is C++ code. In Java there are no pointers. Both examples do the same thing, functionally there is no difference.
I don't know exactly why this was tagged jave as there are no pointers. However, for the concept you can use both pointers and reference interchangeably if you are working on the value attribute. for example i=4 or *i=4. The advantage of using pointers is that you can manipulate the adress itself. Sometimes you will need to modify the adress (point it to something else ...) this is when pointers are different for reference; pointers allow you to work directly on the adress, reference won't
In your example, there is no difference.
In C++ reference is implemented using pointers.
Edit: for those who believe reference can't be NULL:
Guess, what will be the result for
test(0)
?