Results 1 to 2 of 2
- 01-15-2011, 11:49 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
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-15-2011 at 11:52 PM.
-
Duplicate post closed. OP, please only one thread per question.
Similar Threads
-
handling double pointer in Java?
By mattie in forum Advanced JavaReplies: 0Last Post: 01-15-2011, 11:48 PM -
object new and pointers error
By hydride in forum New To JavaReplies: 2Last Post: 04-06-2010, 06:15 PM -
Does Java support pointers?
By kthaker in forum New To JavaReplies: 2Last Post: 10-07-2009, 08:16 AM -
Java pointers? How to...
By Krooger in forum New To JavaReplies: 4Last Post: 11-04-2008, 08:30 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