• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between a reference and a pointer

 
Dóra Takács
Ranch Hand
Posts: 33
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

First of all I'd like to thank everyone on this forum as I've used the search function dozens of times to find more info on topics that were unclear to me in preparation for my OCA java8 exam!
I have been preparing for the exam for over 2,5 months now, and I'm taking it soon, so I'm doing a run-through on all the Review Questions from the Study Guide book.
The following question, I have problems understanding: (Page 49., 22th question, option C, and explanation: Page 336., 22th)

"C: Java has pointers to specific locations in memory" (false)
In the explanation on the correct answers:
"Java does have references to objects, but these are pointing to an object that can move around in memory."

While studying (coming from C/C++ experience) I handled references as a synonym for convenient pointers, so this confused me a lot. I marked answer C true, as I thought, that references are pointers.
So, from a technical standpoint, how can an object move around in memory, while the reference pointing to it does not change? Is it because the JVM handles that where the actual object is, and the reference only tells the JVM that "I just want to reach this object, no matter where it is"?
If so, how can a reference be an actual memory address (as describe in multiple places as a 4byte memory address in 32x systems)? Wouldn't that make it an actual pointer, to the physical location of the object?

I know this is an advanced question I'm not needed to know for the exam, but I take this exam exactly to know the details and delicacies of the language, and not to just get myself certified.

All help and/or reference to another topic is greatly appreciated!
Best regards, Dóri


Quick edit: Added "(false)" after the statement so fellow wanderers around the forum dont interpret it wrong (and think it true) if just looked upon first glance.
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java doesn't have pointers. C and C++ have pointers. Furthermore, the word "reference" means something different in Java and in C++.

In Java, a reference is a handle to an object. You interact with objects through a reference. References can be implemented by pointers, but if Java runs on a crystal ball, references can also be implemented by magic.

In C++, a reference is an alias for a variable. You can change the value of the variable through the reference. In Java you can only change the value of a variable through the variable itself.

In C/C++ a pointer is a datatype that holds a memory address. You can use operators on them so they point to other memory locations. If you run C or C++ on a machine that doesn't use memory (maybe a crystal ball), they won't work.
 
Aaron Shawlington
Ranch Hand
Posts: 50
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In languages that have references & pointers (as mentioned by the poster above) such as C++, then it tends to refer to the fact that a pointer 'points' to a memory address containing a value, while a reference (using the & operator in C++) is as the poster above mentioned - an alias of the value of that pointer (the variable).

What's important to understand here is that that 'alias' of that variable allows you to modify the value of that variable, but not the reference to it (the pointer).
I.e. if you pass a pointer, you then have the option to manipulate that pointer (to make it point somewhere else), or to manipulate the value that that pointer points to. With a reference you can only manipulate the value that reference points to.

There are loads of reasons to use one over the other, but one example to use a reference might be this:

You want to pass a large variable to a function AND you don't want anyone to accidentally make your pointer point to something other than the original variable.

By using a reference you don't have to copy the value to pass into the function - you just pass the memory address of that variable. You could do the same by passing a pointer to that memory address, but then the receiving function could accidentally manipulate that pointer (not it's value) to point to something else. That would mean that other areas of code referencing that original variable would get broken.

 
Dóra Takács
Ranch Hand
Posts: 33
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, both of you were of great help. I see why my answer wasn't correct! I think I will will revise the chapter about references.

Thank again!
 
Campbell Ritchie
Sheriff
Posts: 51361
87
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, an object can move in memory; that is how garbage collection works.
I find that people who have learnt different languages often find it hard to identify the difference when they move to Java®. Some books will tell you the difference, but I can't find mention of references or pointers in the index of my Horstmann: Core Java II, but my earlier 2005 edition has this on page 101:-
Horstmann and Cornell, Core Java 2 vol I p101 wrote:C++ Note: Many people mistakenly believe that Java obect variables behave like C++ references. Butin C++ there are no null references, and references cannot be assigned. You should think of Java object variables as analolous to object pointers in C++. For example,
    Date birthday; // Java
is really the same as
    Date* birthday // C++
Once you make this association, everything falls into place. Of course, a Date* pointer isn't initialised until you initialise it with a call to new. The syntax is almost the same in C++ and Java.
    Date* birthday = new Date(); // C++
If you copy one variable to another, then both variables refer to the same date—they are pointers to the same object. The equivalent of the Java null reference is the C++ NULL pointer.
All Java objects live on the heap. When an object contains another object variable, that variable still contains just a pointer to yet another heap object.
In C++, pointers make you nervous because they are so error prone. It is easy to create bad pointers or to mess up memory management. In Java, these problems simply go away. If you use an unitialised pointer, the runtime system will reliably generate a runtime error instead of producing random results. You don't worry about memory management, because the garbage collector takes care of it.
I hope that helps
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dóra Takács,

First of all, a warm welcome to CodeRanch!

Dóra Takács wrote:While studying (coming from C/C++ experience) I handled references as a synonym for convenient pointers, so this confused me a lot.

I think the people who created the Java Language more than 20 years ago were confused as well, because in Java you have a java.lang.NullPointerException

In the online mock exams of this study guide, you'll find a similar question which confused another rancher as well. And as you can see from my reply in that topic, I agree with all the above replies: Java doesn't have pointers, Java has references. And that makes programming so much easier and safer as you don't have the drawbacks of pointers.

Hope it helps!
Kind regards,
Roel
 
Dóra Takács
Ranch Hand
Posts: 33
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, you are the Bests! :3
After Roel De Nijs' and all of your posts, and links I've found following explanations, I wanted to put here what worked for me quite well, made me sure about things and clarified others:
The Link:
http://programmers.stackexchange.com/questions/141834/how-is-a-java-reference-different-from-a-c-pointer

The explanation by Joachim Sauer:
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.



Agains, thanks everyone for the help, it is greatly appreciated!
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Thanks for getting back to us. Have a cow
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic