Differencebetween a Pointer and a Reference Variable in Java

What is the difference between a pointer and a reference variable in Java?

A reference is sort of like a pointer that you can't do arithmetic on... although it's more opaque. While the underlying bits may be an address in virtual memory, they don't have to be. They're just a way of getting to an object (or representing the null value). So while they're not exactly the same, if you're used to thinking of a pointer as "a way of identifying an object or navigating to it" (in some sense) then yes, those thoughts apply to references too.

Java doesn't have pointers as such (unlike, say, C# which has references and pointers - the latter being used in "unsafe" code).

What's the difference between a Java reference and C++ reference

Java references are much closer to C++ pointers rather than C++ references. In Java, you can do the following with a reference:

  • Change which object it refers to
  • Check whether two references are equal or unequal
  • Send messages to the referenced object.

In C++, pointers have these same properties. As a result, the code you're looking for in C++ is something like

float* f = new float;

Which is perfectly legal. For a better comparison, this Java code:

String myString = new String("This is a string!"); // Normally I wouldn't allocate a string here, but just for the parallel structure we will.
System.out.println(myString.length());

/* Reassign myString to point to a different string object. */
myString = new String("Here's another string!");
System.out.println(myString.length());

would map to this C++ code:

std::string* myString = new std::string("This is a string");
std::cout << myString->length() << std::endl;

delete myString; // No GC in C++!

/* Reassign myString to point to a different string object. */
myString = new std::string("Here's another string!");
std::cout << myString->length() << std::endl;

delete myString; // No GC in C++!

Hope this helps!

What is the real difference between Pointers and References?

It's all just indirection: The ability to not deal with data, but say "I'll direct you to some data, over there". You have the same concept in Java and C#, but only in reference format.

The key differences are that references are effectively immutable signposts - they always point to something. This is useful, and easy to understand, but less flexible than the C pointer model. C pointers are signposts that you can happily rewrite. You know that the string you're looking for is next door to the string being pointed at? Well, just slightly alter the signpost.

This couples well with C's "close to the bone, low level knowledge required" approach. We know that a char* foo consists of a set of characters beginning at the location pointed to by the foo signpost. If we also know that the string is at least 10 characters long, we can change the signpost to (foo + 5) to point at then same string, but start half the length in.

This flexibility is useful when you know what you're doing, and death if you don't (where "know" is more than just "know the language", it's "know the exact state of the program"). Get it wrong, and your signpost is directing you off the edge of a cliff. References don't let you fiddle, so you're much more confident that you can follow them without risk (especially when coupled with rules like "A referenced object will never disappear", as in most Garbage collected languages).

Difference between reference and pointer

A pointer is a reference type; it refers to something. What you're basically asking is: "Does Java have Dobermans? Because some articles say it has dogs."

As noted in Wikipedia entry for Pointer:

A pointer is a simple, more concrete implementation of the more abstract reference data type. Several languages support some type of pointer, although some have more restrictions on their use than others

It goes on to say this about Java specifically:

Unlike C, C++, or Pascal, there is no explicit representation of pointers in Java. Instead, more complex data structures like objects and arrays are implemented using references. The language does not provide any explicit pointer manipulation operators. It is still possible for code to attempt to dereference a null reference (null pointer), however, which results in a run-time exception being thrown. The space occupied by unreferenced memory objects is recovered automatically by garbage collection at run-time.

Looking up Reference you find:

In computer science, a reference is a value that enables a program to indirectly access a particular datum, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the datum, and accessing the datum is called dereferencing the reference.


A reference is distinct from the data itself. Typically, a reference is the physical address of where the data is stored in memory or in the storage device. For this reason, a reference is often called a pointer or address, and is said to point to the data. However a reference may also be the offset (difference) between the datum's address and some fixed "base" address, or an index into an array.

Java chose to use the broader term "reference" instead of "pointer" because of the differences between Java and C. (Thus creating a sisyphus-like situation where we have to keep explaining that Java is pass-by-value).

You don't have a C pointer, you have a Java Reference. This has nothing to do with a C++ reference, or pass-by-reference.

Because Java is pass-by-value it is similar to using a C pointer in that when you pass it to a method, the value (e.g. memory address) is copied.

What is the difference between passing by reference in Java and passing a pointer in C?

Neither Java nor C has pass-by-reference. They are both strictly pass-by-value.

Pass-by-reference semantics mean that when you change the value of the parameter in the method, the caller will see that change in the argument.

Now, you may be thinking: "But that's the case in Java! If I change an object during the method, the caller sees that change." The object isn't the parameter. The parameter is just the variable - and if you change the value of that variable, the caller won't see that. For example:

public void foo(Object x)
{
x = null;
}

public void caller()
{
Object y = new Object();
foo(y);
// y is still not null!
}

If the parameter were really passed by reference, y would be null afterwards. Instead, the value of y is just a reference, and that reference is passed by value. It's confusing because the word "reference" is in both terms, but they're different things.

You may want to look at my article about C# parameter passing to see what would be possible if Java did have pass-by-reference semantics, as C# does when (and only when) you use the ref keyword.

You may also want to look at the comments to my Stack Overflow answer related to the topic.

What is the difference between Address and Reference in Java?

Java spec doesn't introduce definition of address, so if we not talking about JVM implementation there is no such thing.

If we look deeper we can say that difference between address and reference is caused by GC. Specifically by its ability to relocate objects.

Lets say we have object o in memory. Its address is 100500. After GC all references pointing to o will still point to it, but 100500 is not where that object is located now.

This is only my opinion. There is, as I said before, no official definition of address.



Related Topics



Leave a reply



Submit