Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why java doesnot support pointer concept?

 
sriram sundararajan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();"
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I'm sure it does. That's why arrays start at 0 for 0 * elementsize bytes offset from the beginning of the array.
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic