aspose file tools*
The moose likes Beginning Java and the fly likes why java doesnot support pointer concept? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why java doesnot support pointer concept?" Watch "why java doesnot support pointer concept?" New topic
Author

why java doesnot support pointer concept?

sriram sundararajan
Ranch Hand

Joined: Apr 08, 2003
Posts: 43
why java is not supporting pointers. What is the drawback of using it in java? Is it something related memory leak and garbage collection?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Java supports pointers just fine, but they're called object references and always point to some subclass of Object. A point to an int is actually a reference to an Integer object that wraps a primitive int.

What Java doesn't allow is pointer arithmetic. You cannot blindly poke around in memory by altering references. The main reasons for this are safety and security. Pointer arithmetic is usually the source of many hard-to-find bugs, and being able to read/write anywhere makes security (sandboxing) difficult.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Java has lots of pointers but they're all nullpointers
References aren't pointers, they're more like pointers to pointers making for even more pointers


42
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
And Java doesnt support function pointers. Actually it does, function pointers are called Interfaces in Java. Having Interfaces allows the compiler to perform stricter checking during compile time
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Actuallt, a function pointer would be a java.lang.reflect.Method object -- a little bit more typesafe than function pointers (since it contains information about the number of arguments, the parameter types, etc...), but that is all run-time checking -- the compiler won't catch it.

In short, Java does support everything you can do with pointers except add to them. And if your adding pointers together, then you really should think about diffrent ways to acheive the same end. Wht say "int i = myPointer+5;" when you can say "int cardValue = aCard.getCardValue();"


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
That brings up a question. Does the java compiler use pointer arithmetic behind the scenes, for things like walking through arrays?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yes, I'm sure it does. That's why arrays start at 0 for 0 * elementsize bytes offset from the beginning of the array.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Originally posted by Jeroen Wenting:
Java has lots of pointers but they're all nullpointers
References aren't pointers, they're more like pointers to pointers making for even more pointers


Beg your pardon, but write some JNI code.

References are pointers.

Guy
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Actually, you're both correct

References are jvm-specific, but they are not real memory addresses as in C. They are indexes into structures that in turn point to the actual objects.

However, they can do almost everything a C pointer does except be modified arithmetically.


Mike Gershman
SCJP 1.4, SCWCD in process
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jason Fox:
That brings up a question. Does the java compiler use pointer arithmetic behind the scenes, for things like walking through arrays?


Not the compiler (byte code doesn't contain any pointer arithmetic, as far as I know?), but the JVM surely does. How else could it work?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why java doesnot support pointer concept?