-3

I’ve read a lot of articles about how “pass-by-reference” doesn’t exist in Java since a copy of the value of the reference is passed, hence “pass-by-copy-of-reference-value”.

The articles also say a reference value is a pointer. (So pointers do exist in Java.)

Some other articles say: Java has no pointers.

So what is the correct solution?

How does a pointer differ from a reference (or reference value), and do they exist in Java?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Mazzy
  • 1,901
  • 2
  • 16
  • 36
  • A pointer is a type of reference; it *refers* to something. Java chose to use the 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) – Brian Roach Dec 23 '13 at 01:43
  • @tbodt please read my question, it's not a duplicate since my question came after reading tons of articles including that one... there is no way that's the same question.... – Mazzy Dec 23 '13 at 01:45
  • 2
    @Mazzy You're right. This isn't a duplicate. I'll take back my close vote. – tbodt Dec 23 '13 at 01:50
  • @tbodt thanks for reconsidering :) – Mazzy Dec 23 '13 at 01:58
  • The value of an object in java is its' reference address. Java is always pass by value. – Elliott Frisch Dec 23 '13 at 17:41
  • here is another possible duplicate (that references the 'duplicate' referenced in this Q): http://stackoverflow.com/questions/7436581/what-is-the-difference-between-a-pointer-and-a-reference-variable-in-java – GitaarLAB Dec 23 '13 at 17:48

5 Answers5

4

They aren't like C pointers. There's no pointer arithmetic allowed.

Java has only one mechanism for passing parameters: pass by value in all cases. For primitives, the value is passed. For objects, the reference to the object on the heap is passed.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    So they may not be like C pointers, but does C define the word pointers? I quess not so pointers in java still exist right? – Mazzy Dec 23 '13 at 01:43
  • 1
    To be clear: copy of the reference is passed. – MGorgon Dec 23 '13 at 01:43
  • 3
    @MGorgon No, a copy of the reference value is passed, not a copy of the reference itsself. – Mazzy Dec 23 '13 at 01:44
  • 2
    @Mazzy - what's the difference? – Dawood ibn Kareem Dec 23 '13 at 01:44
  • @DavidWallace A reference *holds* a value while a reference value is the value from the reference – Mazzy Dec 23 '13 at 01:46
  • @JeroenVannevel a value to the memory location(ie 3bad086a) – Mazzy Dec 23 '13 at 01:47
  • That's right. So when you change reference in method, reference outside of it will not be changed. – MGorgon Dec 23 '13 at 01:50
  • @Mazzy, I really can't tell from your comments whether you understand this correctly or not. Saying that you don't get a copy of the reference just seems wrong to me - that's absolutely what you get. – Dawood ibn Kareem Dec 23 '13 at 01:51
  • @DavidWallace After reading the 3rd answer from this: http://stackoverflow.com/questions/40480/is-java-pass-by-reference "You're passing the value of the reference and not the reference itself (and not the object)." Is what I said above not true then? – Mazzy Dec 23 '13 at 01:56
  • Umm, I find that answer to be badly worded. It's hard to say whether something is right or wrong, when it's unclear what its intended meaning is. – Dawood ibn Kareem Dec 23 '13 at 01:59
  • @DavidWallace I just feel that a reference is more then just a simple address, so instead of passing the whole reference, you are just passing the value of the reference, the address(I don't know if this is true or not) – Mazzy Dec 23 '13 at 02:03
  • 1
    @Mazzy The thing that goes on the stack, and is therefore "passed" to the method that you're calling, is neither more nor less than the address of the object. It manifests as references in both the calling method and the called method. Really, the reference is nothing more than the address. – Dawood ibn Kareem Dec 23 '13 at 02:47
  • "For objects" There's no "for objects", because "objects" are not values in Java. The only types in Java are primitives and reference types, and thus the only values in Java are primitives and references. No "objects". – newacct Dec 24 '13 at 02:57
0

It is right to say both:) Java has no pointers since java has simplified pointers as references.

Object o=new Object();

We got an object o here; o is actually a pointer.

Basically, pointers and references are the same thing; they point to (refer to) something else in memory. However, you cannot do integer arithmetic on references. You may find some pages on this slide useful: http://www.cis.upenn.edu/~matuszek/cit594-2005/Lectures/15-pointers-and-references.ppt

JJZ
  • 26
  • 2
  • "java has simplified pointers as references" By saying it's simplified, you are implying there's a "not simplified" version to compare against. But who decides what defines a "not simplified" pointer? Who decides that a Java reference is not a "regular" pointer? – newacct Dec 24 '13 at 03:04
0

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.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • So it's not a c pointer, but still some type of pointer? – Mazzy Dec 23 '13 at 02:05
  • 1
    I seriously don't know how to explain it any simpler. It's a type of **reference**. The same way a C pointer is a type of reference. They're siblings. – Brian Roach Dec 23 '13 at 02:12
  • Be sure you are clear about "value (contents) is [sic] copied". Objects live on the heap; you pass a reference that tells you where to go to find the object. The reference value is passed, not an object. – duffymo Dec 23 '13 at 10:27
  • @duffymo dude, seriously... When talking about how its similar to a c pointer and an entire wall of text explaining what a reference is, I think we've covered that – Brian Roach Dec 23 '13 at 17:16
  • @BrianRoach - Dude, stop taking yourself so seriously. You aren't the be all and end all on this topic or this site. – duffymo Dec 23 '13 at 18:00
  • The quoted text has many problems. "there is no explicit representation of pointers in Java" It does not define what is "explicit representation of pointers", so it is meaningless. I would argue that Java has as much "explicit representation of pointers" as C. "Instead, more complex data structures like objects and arrays are implemented using references." Data structures are not "implemented" using references. Data structures are implemented using objects. Objects are pointed to using references. Again, saying that it uses references does not say that it is not a pointer. – newacct Dec 24 '13 at 03:01
0

You have to get your head around the different, but related concepts of types, variables and objects. If we ignore for now the fundamental types like int and only consider class types, then in Java there are variables, which are "named things", and objects. Both variables and objects have a type. However, a variable of type T is not an object; rather, it is a mechanism for locating an object of type T, and for informing the runtime that this object is in use. A variable may at any point not locate any object, in which case it is null, or it may, and in that case the very existence of the variable keeps the object alive.

Let's repeat: Variables have names. Objects don't have names. Variables are not objects.

When you pass a variable as an argument into a function call, the corresponding function parameter becomes duplicate of the argument, so that there are now two variables which both locate the same object. When you assign one variable to another, you make the left-hand variable locate the same object (possibly null) as the right-hand variable, relinquishing the possibly previously held location. But no objects are being affected by this; the objects exist in some unrelated, unprobable plane of existence.

Also, variables have a deterministic lifetime, which is determined by their scope (essentially block-local or static-global). The lifetime of variables is non-deterministically related to the lifetime of objects, but the lifetime of objects cannot be controlled directly.

That's the type system and object model of Java (for class types) in a nutshell. It's up to you what you want to label this; it makes sense to say that "variables are references", since that's what they do, but you might as well just stop trying to compare yourself to other languages and just say "variables", which is clear enough within the context of Java. Variables are variables, objects are objects, neither one is ever the other, and you need the former to talk about the latter.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • This is just a very long-winded way of saying that the value of variables are *pointers to objects*. – newacct Dec 24 '13 at 03:11
0

In Java, a reference is a pointer, usually one that isn't null. That's why it's called NullPointerException, not NullReferenceException. "The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object. "

Java pointers/references are akin to Pascal pointers, not to C or C++ pointers, in that they are very strongly typed and do not support address arithmetic.

user207421
  • 305,947
  • 44
  • 307
  • 483