Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

C has pointers and Java has what is called references. They have some things in common in the sense that they all point to something. I know that pointers in C store the addresses they point to. Do reference also store the address? How they are different except that pointer is more flexible and error-prone?

share|improve this question
7  
note C++ also has references which are different to pointers or java references –  jk. Mar 28 '12 at 8:17
    
@jk. I thought it would be the same as in Java.What's the difference? –  Gnijuohz Mar 28 '12 at 8:20
11  
C++ references aren't rebindable (i.e. you can't change the object designated) and aren't nullable (i.e. you can't validly have them reference no object at all). –  AProgrammer Mar 28 '12 at 8:37

3 Answers 3

up vote 84 down vote accepted

References might be implemented by storing the address. Usually Java references will be implemented as pointers, but that's not required by the specification. They may be using an additional layer of indirection to enable easier garbage collection. But in the end it will (almost always) boil down to (C-style) pointers being involved in the implementation of (Java-style) references.

You can't do pointer arithmetic with references. The most important difference between a pointer in C and a reference in Java is that you can't actually get to (and manipulate) the underlying value of a reference in Java. In other words: you can't do pointer arithmetic.

In C you can add something to a pointer (i.e. the address) or substract something to point to things that are "nearby" or point to places that are at any place.

In Java, a reference points to one thing and that thing only. You can make a variable hold a different reference, but you can't just ask it to point to "the thing after the original thing".

References are strongly typed. Another difference is that the type of a reference is much more strictly controlled in Java than the type of a pointer is in C. In C you can have an int* and cast it to a char* and just re-interpret the memory at that location. That re-interpretation doesn't work in Java: you can only interpret the object at the other end of the reference as something that it already is (i.e. you can cast a Object reference to String reference only if the object pointed to is actually a String).

Those differences make C pointers more powerful, but also more dangerous. Both of those possibilities (pointer arithmetic and re-interpreting the values being pointed to) add flexibility to C and are the source of some of the power of the language. But they are also big sources of problems, because if used incorrectly they can easily break assumptions that your code is built around. And it's pretty easy to use them incorrectly.

share|improve this answer
12  
+1 for might. Don't rely on implementation details. –  Michael Kjörling Mar 28 '12 at 9:52
1  
+1 Doesn't garbage collection deserve mention as a specific bold point? It's another way that C pointers are more powerful but also more dangerous (risk of dangling pointers to freed memory causing memory corruption, risk of memory leaks) –  MarkJ Aug 14 '13 at 11:07
    
Another difference between references and pointers is that a pointer in C may be converted into a sequence of numbers (e.g. by using memcpy to move one into a char[]) and vice versa. If a pointer is converted to a sequence of numbers which is stored someplace (maybe shown on screen and copied down by the operator on a slip of paper), all copies of the pointer within the computer are destroyed, and that sequence of numbers is converted back to a pointer (perhaps after being typed in by the operator), the pointer must still point to the same thing it did before. A program which... –  supercat Jan 8 at 19:22
    
...displayed a pointer as numbers, and then converted manually-entered numbers to a pointer, might be "evil", but it would not invoke any Undefined Behavior unless the operator typed in numbers that hadn't been shown to form a valid pointer. General-purpose garbage collection is thus impossible in fully portable C, because there's no way the computer can know whether a copy of a pointer might exist somewhere in the universe. –  supercat Jan 8 at 19:24

C++ references are different again.

They have to be initialized and can't be null (at least not in a well formed program) and can't be reseated to refer to something else. a C++ reference is much more like an alias for an object.

Another important difference between pointers and Java/C++ references is that you can take the address of a pointer you cannot access the address of a reference (indeed a C++ reference need not actually exist as an object in memory at all) consequently you can have a pointer to a pointer but not a reference to a reference

share|improve this answer

They're really not different, in the sense that the memory occupied the variable contains the memory location of the object itself.

In C, however, a variable doesn't have to be a pointer - if you have a variable whose value is a pointer, you can have another variable whose value is the thing being pointed to.

You can't do this in Java - there's no way to make a variable whose value is the object itself (as opposed to a reference/pointer to the object). In fact, Java even uses the term "Null Pointer Exception" for what happens when you try to use a variable which is a reference to nothing, as if it were a reference to something.

share|improve this answer
    
There are multiple things wrong with this. A Java reference doesn't necessarily (directly) contain the address of an object (see Joachim's answer). In Java, a variable also doesn't have to be a reference. Primitive type variables contain the value itself, not a reference to the value. –  Jesper Mar 28 '12 at 12:48
1  
The first point: that's about the difference in theory and practice. I don't know if there's no such JVM. Oracle's JVM is by far not the only JVM implementation out there. About the second point, maybe you should have pointed out that you're not talking about primitive variables. –  Jesper Mar 29 '12 at 5:24
1  
Yes, but that's like arguing that since an eight-legged elephant is theoretically possible, "elephants have four legs" is an invalid answer to "what's one difference between an elephant and a spider". Maybe I should clarify that I am speaking about what HAPPENS, not what is allowed by the spec. Umm, and I could point out that primitive variables work differently from object variables in Java; whereas in C they're more similar to each other. I just thought it was clear from the context that primitives were outside of the discussion. In any case, thanks again for the feedback. –  David Wallace Mar 29 '12 at 6:27
3  
@DavidWallace - The Kissme JVM (now defunct as a project) used indirect pointers for references. What we are talking about is not just theoretical. –  Stephen C Aug 23 '13 at 4:37
2  
HotSpot itself occasionally uses "not-really-an-address" as the value it stores for references: Compressed OOPS are a way to store references in 32bit, when running in 64 bit mode. –  Joachim Sauer Aug 23 '13 at 5:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.