Results 1 to 2 of 2
  1. #1
    mattie is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default Handling double pointers in Java?

    Hi:
    How would you handle situations when you need a C++ double pointer in Java?

    e.g. Imagine we necessarily wanted to use the "new" operator in func2(). How would we be able to send the newly created object back to func1()?
    Note that since parameter "b" in func2() is NOT sent by a double pointer, here is what happens in func2(): temporary pointer "b" contains the address of original object B created in func1(). Then new object B is created and the temporary pointer "b" will contain its address, pointing to the new object, however, the real pointer "b" in func1() will not point to anything different as the temporary pointer "b" is only a copy of pointer "b" in finc1().

    class B
    {
    public int b1;
    public B( int b1 ) { this.b1 = b1; }
    }
    class A
    {
    public void func1()
    {
    B b = new B( 0 );
    func2( b );
    System.out.println( String.valueOf( b.b1 ); //will print "0" ( not "2" )
    }
    public void func2( B b )
    {
    b = new B( 2 );
    //b.b1 is now 2, but this new object will be lost as soon as we are out
    //of this method
    }
    }
    Last edited by mattie; 01-16-2011 at 12:52 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. handling double pointer in Java?
    By mattie in forum Advanced Java
    Replies: 0
    Last Post: 01-16-2011, 12:48 AM
  2. object new and pointers error
    By hydride in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 07:15 PM
  3. Does Java support pointers?
    By kthaker in forum New To Java
    Replies: 2
    Last Post: 10-07-2009, 09:16 AM
  4. Java pointers? How to...
    By Krooger in forum New To Java
    Replies: 4
    Last Post: 11-04-2008, 09:30 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
  •