pointers in java

N

New

Is it possible to have pointer in java (like C) so that an object can point
to another object it can point back to it without have the whole other
object inside the object because it becames an infinite loop?

Thank you
 
C

Christophe Vanfleteren

New said:
Is it possible to have pointer in java (like C) so that an object can
point to another object it can point back to it without have the whole
other object inside the object because it becames an infinite loop?

Thank you

Hmm, lots of confusion here.

First of all, Java doesn't have pointers, but references. You can do less
with references than with pointers.

I'm guessing that you're asking about wether it's ok to have a circular
references, as in

A has a references to B, which has a reference to A.

This will work without a problem, an will allow both A and B to get garbage
collected, provided you have no other references to A or B.
 
T

Timo Kinnunen

Is it possible to have pointer in java (like C) so that an object can point
to another object it can point back to it without have the whole other
object inside the object because it becames an infinite loop?

Yes, Java's references match pointers pretty closely in this respect.
 
R

Roedy Green

Is it possible to have pointer in java (like C) so that an object can point
to another object it can point back to it without have the whole other
object inside the object because it becames an infinite loop?

yes anything can point to anything in loops. An object can point to
itself.

This causes no problem unless you CHASE the chains endlessly.

see http://mindprod.com/jgloss/reference.html
 
M

Mike Schilling

Christophe Vanfleteren said:
Hmm, lots of confusion here.

First of all, Java doesn't have pointers, but references. You can do less
with references than with pointers.

That is, Java calls its pointers "references", probably because C and C++
are so familiar these days that calling them "pointers" would imply that
they supported C-like pointer arithmetic. In an alternate would in which
Pascal had the sort of success that C has had, the word "pointers" would
describe Java references quite well.
 
T

Tony Morris

This will work without a problem, an will allow both A and B to get
garbage
collected, provided you have no other references to A or B.

This is not guaranteed i.e. implementation specific behaviour.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
A

Andrew Hobbs

Tony Morris said:
This is not guaranteed i.e. implementation specific behaviour.

Can you explain what you mean here. Surely simple circular references, A to
B and B to A, will not affect anything unless some method(s) creates a
circular execution path.

And if there are no references then they should be eligible for GC.

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top