Results 1 to 3 of 3
  1. #1
    kthaker is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default Does Java support pointers?

    Hi All,

    I would like to know whether Java supports pointers or not..As far as I know it does not have pointers but I don't know the exact reason why it do not support. Please let me know the reason.

    Thanks.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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.

    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;
    }
    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.

    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

  1. How does java support polymorphism?
    By makpandian in forum New To Java
    Replies: 3
    Last Post: 06-26-2009, 03:52 AM
  2. pointers and wrapper classes
    By becky in forum New To Java
    Replies: 11
    Last Post: 02-07-2009, 04:59 AM
  3. Java pointers? How to...
    By Krooger in forum New To Java
    Replies: 4
    Last Post: 11-04-2008, 09:30 PM
  4. JAVA support to LAPD
    By syedumairali in forum New To Java
    Replies: 3
    Last Post: 01-26-2008, 03:49 PM
  5. Pointers
    By ravian in forum New To Java
    Replies: 5
    Last Post: 11-28-2007, 02:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •