Results 1 to 3 of 3
Thread: Does Java support pointers?
- 10-07-2009, 07:00 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
- 10-07-2009, 07:14 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I would like to know whether Java supports pointers or not.
Yes and no.
You can't do anything with memory addresses in Java. Including, notably, pointer arithmetic of the *p++ variety.
You can dereference things - indeed it's hard to do anything nontrivial without using the dot operator. The things you dereference are variously called "references" (very helpful!) and "pointers to objects" (see for example the JLS 4.3.1). This latter description gets at the fact that Java's foo.bar() somewhat resembles (*foo).bar() or foo->bar().
Please let me know the reason.
I have no idea about this. Who would, except for the language's authors? Maybe someone has a link to an account of what caused or motivated them to act as they did.
- 10-07-2009, 08:16 AM #3
Object references are treated like pointers where as primitives are passed by value. Direct memory addressing makes little sense in a virtual environment, which java is.
If in this case java was not using a pointer (pass by value), we would expect the print out to be 4, since b was initialized with a's value. Since b is actually a reference to a itself, incrementing a's value also affects b. Note that this is not true with primitives, Strings, or the primitive wrapper classes.Java Code:public void test(){ Ob a = new Ob(); Ob b = a; a.val++; System.out.println(b.val); } class Ob{ public int val = 4; }
I use the term point somewhat loosely since in java (as I mentioned) you cannot deal with actual memory addresses directly, however, if pointer is defined as multiple fields 'pointing' at the same memory location, then java does use 'pointers' for objects.
Similar Threads
-
How does java support polymorphism?
By makpandian in forum New To JavaReplies: 3Last Post: 06-26-2009, 02:52 AM -
pointers and wrapper classes
By becky in forum New To JavaReplies: 11Last Post: 02-07-2009, 03:59 AM -
Java pointers? How to...
By Krooger in forum New To JavaReplies: 4Last Post: 11-04-2008, 08:30 PM -
JAVA support to LAPD
By syedumairali in forum New To JavaReplies: 3Last Post: 01-26-2008, 02:49 PM -
Pointers
By ravian in forum New To JavaReplies: 5Last Post: 11-28-2007, 01:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks