• Post Reply Bookmark Topic Watch Topic
  • New Topic

java and pointers  RSS feed

 
Puneet N Vyas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as we know object references are implemented via pointers,then why we don't have access of pointers in java...
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fundupuneet vyas:
as we know object references are implemented via pointers,then why we don't have access of pointers in java...


There are many reasons, but the two big ones are probably....

Security. You can't implementment the security model needed for applets. Or even needed in a shared model used in application servers, if an application can do pointer arithmetic to access portions of memory that they are not allowed to touch.

Garbage Collection. It would be impossible to detect whether something has been dereferenced, if the program can convert a pointer to an integer, so that it can calculate the reference later.


Have said that... as you already mentioned ... References are like pointers -- you just can't do pointer arithmetic with them.

Henry
[ October 05, 2007: Message edited by: Henry Wong ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that java references do no HAVE to be implemented via pointers, though. anyone can write a JVM that uses some other method. it's possible then than pointer arithmatic wouldn't make any sense.
 
Puneet N Vyas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok,but tell me how could the java references were implemented there are pointers that are used to implement that means we can't deny that java doesn't support pointers,is it right??,but we don't have access to it....
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it kind of depends on what you mean by "support pointers". if you mean "deep in the bowels of the JVM, somewhere, pointers are used by AND ONLY BY the JVM", then yes, you could say java does support pointers.

but if you mean "the jvm uses pointers and developers have access to them and can do pointer arithmatic on them", then NO, java does NOT support pointers.

most people (i believe) would generally use the second definition. in that case, java does NOT support them.
 
Mladen Grabowsky
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fundupuneet vyas:
as we know object references are implemented via pointers,then why we don't have access of pointers in java...

Why would you want pointers in Java?

Is there anything you can't do with references?
Except of course error prone pointer arithmetics which you wouldn't want to do anyways...
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mladen Girazovski:
Is there anything you can't do with references?


Yes, direct access to system resources, memory-mapped I/O etc. But those are the sorts of things that Java deliberately does not let you get at (except by JNI), to provide the higher level of security that Java has, compared to languages like C/C++.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:

Yes, direct access to system resources, memory-mapped I/O etc. But those are the sorts of things that Java deliberately does not let you get at (except by JNI), to provide the higher level of security that Java has, compared to languages like C/C++.


The NIO libraries provide access to MMAP files -- you can do this from within Java (without any JNI).

The NIO libraries also provide access to direct buffers, these are buffers which can't be moved by the GC. This is so that it can be passed to JNI, and used as a communication space between Java and C/C++.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!