why java does not support pointers

C

chandu

hai
any one tell me why java doesn't support pointers.instead it will do
the same(means storing address,not arithematic operations on address)
using reference variables.can any one plese clarify my doubt
thanks regards
chandu
 
L

Luke Meyers

Well, it's debatable whether Java does or doesn't support pointers.
Some would say that everything (well, every reference type) in Java is
a pointer -- hence, the ever-familiar NullPointerException. But
certainly, these reference types do not give one the sort of direct
interaction with memory that we're familiar with from pointers in,
e.g., C. The decision to have it this way has to do with the design
principles of Java. Security and portability are probably two of the
main ones. In Java, can you access uninitialized or deallocated
memory? Well, that's because of the lack of pointers. Like it or lump
it, that's Java.

Luke
 
R

ricky.clarkson

The Java Language Specification defines a reference as:

"An object is a class instance or an array.

The reference values (often just references) are pointers to these
objects, and a special null reference, which refers to no object."

From
http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#106237

Java supports pointers.

However, Java doesn't support pointer arithmetic (well, it does, as of
Java 1.5, but I'm not interested in telling you how). It doesn't
support converting an int to a pointer either.

This is much better, because we can now write code that won't cause
segfaults or General Protection Faults. Instead we get a nasty old
NullPointerException, for different reasons.

Recent Usenet addicts will note that I haven't taken this delightful
opportunity to post a link to a certain article..

Cheers.
 
M

Mike Schilling

chandu said:
hai
any one tell me why java doesn't support pointers.instead it will do
the same(means storing address,not arithematic operations on address)
using reference variables.can any one plese clarify my doubt
thanks regards

Java references are pointers, but they're not much like C or C++ pointers,
so a different term was used. (Of course, Java references are nothing like
C++ references either...)
 
R

Roedy Green

any one tell me why java doesn't support pointers.instead it will do
the same(means storing address,not arithematic operations on address)
using reference variables.can any one plese clarify my doubt
thanks regards

Java has references. They are just tame pointers under the hood.
 
R

Roedy Green

It's all about the sun.misc.Unsafe class, which you shouldn't use in
code you intend to be portable.

Think about how serialisation builds new objects. Have a peek at the
code.
 
S

Stefan Ram

Roedy Green said:
Java has references. They are just tame pointers under the hood.

There is not even a »hood«; JLS3 explicitly states

»(...) reference values (...) are pointers«

in the second sentence of 4.3.1.
 
H

HalcyonWild

chandu said:
hai
any one tell me why java doesn't support pointers.instead it will do
the same(means storing address,not arithematic operations on address)
using reference variables.can any one plese clarify my doubt

Yes, java does not allow pointer arithmatic, but you can consider
references in Java to be as good as C++ pointers, rather than C++
references. Pointer arithmatic is a source of many hard-to-find bugs.
Java was designed to be "safe", and hence the need to remove extra
complexity.
 
L

Luke Meyers

HalcyonWild said:
Yes, java does not allow pointer arithmatic, but you can consider
references in Java to be as good as C++ pointers, rather than C++
references.

Well, Java sacrifices some flexibility for safety. Java references are
more limited than C++ pointers in more ways than one.
Pointer arithmatic is a source of many hard-to-find bugs.

When used improperly. Pointer arithmetic is a mechanism appropriate to
low-level memory manipulation. If that's the domain you're working in,
it's the perfect tool. Problems arise when people use the right tool
for the wrong job. Java abstracts most aspects of memory management
away from you, so there's real need for this kind of manipulation.
Java was designed to be "safe", and hence the need to remove extra
complexity.

s/complexit/efficienc/

;)

Luke
 
R

Roedy Green

»(...) reference values (...) are pointers«

Actually they are not necessarily. They are sufficiently abstract
they would be implemented with handles, pointers to pointers.

The fact that NullPointerException still exists indicates the term
"reference" came late in the game to avoid confusion with C's wild
pointers. The problem is that Java bashers use this to try to fool
people into thinking that Java can't do the things you do with
pointers in C. You can do nearly everything but tinker with platform
specific hardware, e.g. vga REGEN buffers.
 
S

Stefan Ram

Roedy Green might have written, quoted or indirectly quoted
something like:
Actually they are not necessarily. They are sufficiently abstract
they would be implemented with handles, pointers to pointers.

By your own definition, handles are also pointers (because
pointers to pointers are pointers.)

To me, the JLS /defines the meaning/ of words regarding the
field of the programming language Java. So, to me, a "pointer"
in Java is as the word "pointer" is used in the JLS.

For example, an "object" in C++ in something quite different
to an object in Java. So to know what an "object" is in Java,
I'd have to consult the JLS.

So when the JLS said as quoted above, to me, in /can not be
wrong/ by my assumption that the JLS defines the terminology
for Java.
The problem is that Java bashers use this to try to fool people
into thinking that Java can't do the things you do with
pointers in C.

Just today I had a pointer-problem that I could have solved
easily in C, but I was not able to solve in Java.

I was building five tuples using my own tuple class. The
code was shortened for this posting to:

final Tuple t0 = new Tuple( "alpha", "beta" );
final Tuple t1 = new Tuple( "alpha", "gamma" );
final Tuple t2 = new Tuple( "alpha", "beta" );
final Tuple t3 = new Tuple( "alpha", "beta" );
final Tuple t4 = new Tuple( "alpha", "delta" );

Now, I wanted to try my new "Identificator". It is supposed
to return the same reference for two equal objects. So:

TupleIdentificator tupleIdentificator = new TupleIdentificator();

and then

final Tuple v0 = tupleIdentificator.valueOf( t0 );
final Tuple v1 = tupleIdentificator.valueOf( t1 );
final Tuple v2 = tupleIdentificator.valueOf( t2 );
final Tuple v3 = tupleIdentificator.valueOf( t3 );
final Tuple v4 = tupleIdentificator.valueOf( t4 );

Now the references v0, v2, and v3 are expected to be equal,
while the references v1 and v4 should differ. To get a quick
debug output, in C, I'd written:

printf( "%p\n",( void * )v0 );
printf( "%p\n",( void * )v1 );
printf( "%p\n",( void * )v2 );
printf( "%p\n",( void * )v3 );
printf( "%p\n",( void * )v4 );

What would be the equivalent in Java to get a quick debug
output of the five reference values?

I am aware of "System.identityHashCode", but this still gives
me no strict guarantee to have the same behavior as the
actual references in this regard.

So, yes, Java supports pointers, but it does not seem to
support converting them to any external representation.
 
C

Chris Smith

Stefan Ram said:
Roedy Green might have written, quoted or indirectly quoted
something like:

By your own definition, handles are also pointers (because
pointers to pointers are pointers.)

To me, the JLS /defines the meaning/ of words regarding the
field of the programming language Java. So, to me, a "pointer"
in Java is as the word "pointer" is used in the JLS.

Indeed. It would be hard, anyway, to define "pointer" as excluding the
indirect pair of memory addresses that Roedy calls "handles". After
all, C and C++ are both also defined so as to permit such an
implementation of pointers.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Uppal

Stefan said:
What would be the equivalent in Java to get a quick debug
output of the five reference values?

I am aware of "System.identityHashCode", but this still gives
me no strict guarantee to have the same behavior as the
actual references in this regard.

Slightly off-topic, but...

If I really only wanted a quick hack for debugging then I'd be quite happy to
depend on identityHashCode(), if not -- i.e. I needed a /guaranteed/ unique
identifier associated with each of my objects -- then I'd code that explicitly.

-- chris
 
R

Roedy Green

Indeed. It would be hard, anyway, to define "pointer" as excluding the=20
indirect pair of memory addresses that Roedy calls "handles". After=20
all, C and C++ are both also defined so as to permit such an=20
implementation of pointers.

Pointer vs handle was pretty firmly established terminology when I was
working on Pascal Lisa/Mac development circa 1980. You were constantly
aware of the difference since you needed handles to find something and
pointers work on it. Your pointers went stale during a memory
allocation/shuffle, but your handles did not.

I have also seen the term handle used for an index into a table
rather than an address. e.g. in DOS.

In C, a pointer's value is the machine address of the thing pointed
to, even if another pointer. It has no internal hidden indirection.

A reference is a woolier term meaning "a set of bits that will find
you the corresponding object". The precise mechanism by which it does
so is unspecified as are the meaning of the bits in the reference.

Potentially a reference could encode facts about the object or could
be several layers of indirection, e.g. to an out of ram backup.
Because all details of how it works are hidden, that gives maximum
flexibility for implementors. In Sun's current JVM a reference is
just the machine address of the object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top