• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why java doesnot support pointer concept?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();"
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic