January 09, 2003
No pointers in Java?

Jeremy discovered the ever popular, friendly and loveable NullPointerException. Hey, now you can call yourself a Java programmer! (Okay, so it doesn't take much).

It's a common misconception that Java has no pointers. It's not exactly true. Java has pointers, they're just not explicit. Every object is a reference to a location making it a pointer... you just can't manipulate the pointers directly.

Which approach is better.. explicit or not? Well, that very much depends on the context and scope of the application. I'm sure everyone who ever went through learning C will however agree that non-explicit pointers are sure as hell easier to comprehend for someone who is just learning programming.

Here's why it's a pointer.. say you have two Strings s and t

String s = "hi"; String t = "hello";

These are two separate objects in two different locations.

t = s;

Now t and s both reference the same location hence referencing the same object. That is a behavior of a pointer. Now for the purists (hi Steve) that will argue that this is still not a pointer.. oh well.. it's all based on how you define it.

Posted January 09, 2003 10:31 PM in Java
TrackBack URL for this entry: http://www.unix-girl.com/mt/mt-tb.cgi/479
Comments
On January 10, 2003 03:00 AM Nadeem added:

I prefer to call this a reference. A pointer is not a pointer without pointer arithmetic.

#
On January 10, 2003 04:49 AM heap added:

Although Nadeems reply more or less reflects my opinions on this matter, I think pointer arithmetic isn't an essential requirement for pointers. The cyclone developers seems to agree with me.

#
On January 10, 2003 07:56 AM kasia added:

Pascal has pointers w/o direct manipulation as well.. and it was developed before C.. so claiming that a pointer is not a pointer w/o arithmetic is in fact erroneous.

#
On January 10, 2003 09:31 AM Nadem added:

I am not "claiming that a pointer is not a pointer w/o aithmetic"; that's just my opinion, i do not FEEL that a pointer is a pointer if i can't do pointer arithmetics on it.

#
On January 10, 2003 09:55 AM Dan Isaacs added:

I remember the day I was able to call myself an NT Admin. It was when I figured out how to press the reset button on the servers.

#
On January 10, 2003 10:01 AM kasia added:

I don't feel much about pointers :)

#
On March 7, 2003 03:39 PM George added:

I believe that these are pointers because it is possible to use them to make a data structures however I don't like that there aren't any regular nonpointer objects. This can cause uneccesary confusion when using the assignment operator and therefore I prefer to use C++ instead.

#
On June 8, 2003 11:22 PM adam added:

there aren't pointers from the java programmer's perspective, only references. The java implementers are the ones dealing with pointers, since the vm and api are implemented in c/c++.

if you're actually interested in this, read the white papers on the java system.
http://java.sun.com/docs/white/index.html

of interest should be the Garbage Collection and the VM handling of references.

#
On June 30, 2003 11:06 AM Sam added:

"Here's why it's a pointer.. say you have two Strings s and t
String s = "hi"; String t = "hello";
These are two separate objects in two different locations.
t = s;
Now t and s both reference the same location hence referencing the same object."

Doesn't make it a pointer, try to offest into it .. it won't happen. The JVM uses pointers internally not externally.

#
On July 13, 2003 04:57 PM dave added:

>Now t and s both reference the same location hence referencing the same object.

There are several objections to this:

1) It's circular reasoning. "The same location" is a pointery sort of concept. By even thinking of "location of an object" you have already assumed the concept of "address", which is to say, "pointer".

2) How can you tell? You might happen to know that a given implementation stores the string values in the same store location, but is there anything you can do in the language to discover that?

(I'm not a Java programmer)

In C++, which manifestly does have "pointers", this is explicitly stated as implementation-defined behaviour. The two strings are different objects with the same value and that's all you're entitled to know.

3) It all sounds like angels-on-the-head-of-a-pin talk anyway.

Given (in C)
int i, j;
i = 3;
j = 3;

do the two objects have 'the same' value of 3 or 'different' values of 3 or not? I don't know - what does it matter? Do they have the same 'location'? I don't know: they could have any number of locations at all, including none, depending on what my optimizer was able to figure out.

#
On July 16, 2003 10:32 PM magenta added:

My extremely late contribution to the fray... :D

To me, a pointer is a reference which is explicitly dereferenced. Pascal's pointers have explicit reference/dereference, whereas Java's are all implicit. C++ provides both forms, and the explicit ones are called pointers whereas the implicit ones are called references. Go figure.

That said, Java's references have pointer semantics, not reference semantics. That is, if you assign a = b, then it changes the reference a, rather than the value of a. That is how pointers behave. References typically require that you explicitly change the reference; the implied action is by value. Though this is where things get a bit murky; LISP's references can be interpreted either way depending on how you choose to map a functional language to a procedural mentality. (Since in pure LISP you can't even reassign a reference anyway; LISP's references were originally for memory savings, not for allowing side-effects, which is anathema to functional programming. Of course, all modern dialects do have things like setq and so on.)

#
On September 30, 2003 10:01 AM Manjul Mayank Dube added:

I think that in Java we use pointers in a different way.
We use it implicitlly not explictlly,which change the mode of working.
In Java pointers are used by passing refference not bye passing address directly ,which is different from other languages like c or c++.

#
On November 29, 2003 03:39 PM mukul added:

...but its still not a pointer...a pointer has to be manipulatable by the client....this is just a basic referencing system used by the OS eh?

#
On December 18, 2003 09:28 AM FAISAL ABRAR added:

PLZ TELL ME THAT WHAT IS THE ALTERNATE OF C POINTERS AND DELAY() FUNCTION IN JAVA.

#
Trackbacks